The Componere\Definition class
(Componere 2 >= 2.1.0)
简介
The Definition class allows the programmer to build and register a type at runtime.
Should a Definition replace an existing class, the existing class will be restored when the Definition is destroyed.
类摘要
     
      final
      class Componere\Definition
     
     
     
      extends
       Componere\Abstract\Definition
     
     {
    /* Constructors */
    
    
    /* 方法 */
    
    
    /* 继承的方法 */
    
   }目录
- Componere\Definition::__construct — Definition Construction
 - Componere\Definition::addConstant — Add Constant
 - Componere\Definition::addProperty — Add Property
 - Componere\Definition::register — Registration
 - Componere\Definition::isRegistered — State Detection
 - Componere\Definition::getClosure — Get Closure
 - Componere\Definition::getClosures — Get Closures
 
  +添加备注
  
用户贡献的备注 1 note
  
  
  ASchmidt at Anamera dot net ¶
  
 
6 years ago
  Sometimes we might wish to extend a third party class that has been defined as "final". 
In this example, the a child class will extend a "final" class, adding a method. An object instance will be able to access parent members, and is recognized as an instance of both the dynamically created child class as well as its parent class.
<?php
declare(strict_types=1);
/*
 *  Final class would normally prevent extending.
 */
final class ParentC
{
    public $parentvar;
    public $secondvar;
    function __construct() { echo( "\r\n<br/>".$this->parentvar = 'set by '.get_class().'->parentconstruct' ); }
    function parentf() { echo( "\r\n<br/>".get_class().'->parentf >> '.$this->parentvar ); }
}
/*
 *  Dynamically define a child class "DynamicC" 
 *  which successfully extends final class "ParentC".
 */
$DefC = new \Componere\Definition( 'DynamicC', 'ParentC');
// Extend child class with method 'DynamicC::dynamicf()'.
$DefM = new Componere\Method( function( $parm = null ) {
// Populate a parent class property.
$this->secondvar = empty( $parm ) ? 'set by '.get_class().'->dynamicf' : $parm;
// Access an inherited property set by parent constructor.
echo( "\r\n<br/>".get_class().'->dynamicf >> '.$this->parentvar ); 
} );
$DefC->addMethod( 'dynamicf', $DefM );
// Take dynamic child class 'life'.
$DefC->register();
/*
 *  Instantiate the dynamic child class,
 *  and access its own and inherited members.
 */
$dyno = new DynamicC;
$dyno->parentf();
$dyno->dynamicf( 'myvalue ');
// Our object is also recognized as instance of parent!
var_dump( $dyno instanceof DynamicC, $dyno instanceof ParentC );
var_dump( $dyno ); 
%>
will output:
set by ParentC->parentconstruct 
ParentC->parentf >> set by ParentC->parentconstruct 
DynamicC->dynamicf >> set by ParentC->parentconstruct
(boolean) true
(boolean) true
object(DynamicC)
  public 'parentvar' => string 'set by ParentC->parentconstruct' (length=31)
  public 'secondvar' => string 'myvalue ' (length=8)备份地址:http://www.lvesu.com/blog/php/class.componere-definition.php