note 56974 deleted from language.oop5.autoload by cmb

From: Date: Sun, 29 Dec 2019 10:16:41 +0000
Subject: note 56974 deleted from language.oop5.autoload by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-213667@lists.php.net to get a copy of this message
Note Submitter: php at kaiundina dot de ---- The autoload-feature allows to add the behavior of static constructors (like in C#). Static constructors should be called on the first occurence of a class reference - typically a 'new' operator or a static call to a class's operation. They can be used used to initialize complex static properties. And here is an easy and save way how it can be done: Content of MyClass.class.php5: <?php // demo class persisting of a static and a dynamic constructor class MyClass { // static constructor operation public static function _construct() { // just say hello echo '<div>static constructor</div>'; } // default dynamic constructor operation public function __construct() { // just say hello echo '<div>dynamic constructor</div>'; } } ?> Content of index.php5: <?php // declare handler for any unknown class request function __autoload($aClassName) { // load the class require_once ($aClassName . '.class.php5'); // create a reference to the static constructor's operation $staticConstructorReference = array($aClassName, '_construct'); // if the static constructor was declared properly... if (is_callable($staticConstructorReference)) { // call the static constructor call_user_func($staticConstructorReference); } } // create an example object to see both constructors being executed $article = new MyObject(); ?>

« previous php.notes (#213667) next »