note 61691 deleted from language.oop5.decon by levim
| From: | levim@php.net | Date: | Tue, 18 Mar 2014 02:10:51 +0000 |
| Subject: | note 61691 deleted from language.oop5.decon by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198923@lists.php.net to get a copy of this message | ||
Note Submitter:
----
(Refering to: caliban at darklock dot com)
To force a constructor always to be called, and still be able to define a constructor on a derived
class use the model below. Ideal for module architectures, because you only have to know the file
and classname to construct an object.
<?php
class Parameter {}
abstract class BaseClass
{
protected $param;
public final function __construct( Parameter $param )
{
$this->param = $param;
$this->pseudoConstruct();
}
protected abstract function pseudoConstruct();
}
class ConcreteClass extends BaseClass
{
protected function pseudoConstruct()
{
echo __CLASS__.' constructor';
}
}
$refl = new ReflectionClass( 'ConcreteClass' );
if( !$refl->isSubclassOf( 'BaseClass' ) ) throw new Exception( 'Invalid base
class!' );
$refl->newInstance( new Parameter() );
?>