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 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 ); }
}
$DefC = new \Componere\Definition( 'DynamicC', 'ParentC');
$DefM = new Componere\Method( function( $parm = null ) {
$this->secondvar = empty( $parm ) ? 'set by '.get_class().'->dynamicf' : $parm;
echo( "\r\n<br/>".get_class().'->dynamicf >> '.$this->parentvar );
} );
$DefC->addMethod( 'dynamicf', $DefM );
$DefC->register();
$dyno = new DynamicC;
$dyno->parentf();
$dyno->dynamicf( 'myvalue ');
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)