PHP 8.1.0 RC 4 available for testing

La clase Componere\Definition

(Componere 2 >= 2.1.0)


La clase Definition permite al programador construir y registrar un tipo en tiempo de ejecución.

En caso de que una Definition reemplace una clase existente, la clase existente será restaurada cuando la Definition sea destruida.

Sinopsis de la Clase

final class Componere\Definition extends Componere\Abstract\Definition {
/* Constructores */
public __construct(string $name)
public __construct(string $name, string $parent)
public __construct(string $name, array $interfaces)
public __construct(string $name, string $parent, array $interfaces)
/* Métodos */
public addConstant(string $name, \Componere\Value $value): Definition
public addProperty(string $name, \Componere\Value $value): Definition
public register(): void
public isRegistered(): bool
public getClosure(string $name): \Closure
public getClosures(): array
/* Métodos heredados */
public Componere\Abstract\Definition::addInterface(string $interface): Definition
public Componere\Abstract\Definition::addMethod(string $name, \Componere\Method $method): Definition
public Componere\Abstract\Definition::addTrait(string $trait): Definition

Tabla de contenidos

add a note add a note

User Contributed Notes 1 note

ASchmidt at Anamera dot net
3 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.


*  Final class would normally prevent extending.
final class ParentC
__construct() { echo( "\r\n<br/>".$this->parentvar = 'set by '.get_class().'->parentconstruct' ); }
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'.

*  Instantiate the dynamic child class,
*  and access its own and inherited members.

$dyno = new DynamicC;
$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
->parentf >> set by ParentC->parentconstruct
->dynamicf >> set by ParentC->parentconstruct

(boolean) true
(boolean) true

'parentvar' => string 'set by ParentC->parentconstruct' (length=31)
'secondvar' => string 'myvalue ' (length=8)
To Top