note 56974 added to language.oop5.autoload

From: Date: Tue, 20 Sep 2005 18:42:11 +0000
Subject: note 56974 added to language.oop5.autoload
Groups: php.notes 
Request: Send a blank email to php-notes+get-95491@lists.php.net to get a copy of this message
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(); ?> ---- Manual Page -- http://www.php.net/manual/en/language.oop5.autoload.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56974 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56974&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56974&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56974&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56974&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56974&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+56974&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56974&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56974&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95491) next »