note 89173 added to language.oop5.autoload

From: Date: Wed, 25 Feb 2009 11:31:19 +0000
Subject: note 89173 added to language.oop5.autoload
Groups: php.notes 
Request: Send a blank email to php-notes+get-151097@lists.php.net to get a copy of this message
I've been playing with the idea of automatically running the __construct() function after initiating a static/singleton class. As we know, it doesn't run unless you create new instance() of the class which doesn't happen on the singleton method. I've seen the methods of using the "getInstance" call but this is a manual call that I wanted to avoid. As an additional idea I've done this and would be interested in opinions. Seems to work for me and my framework and it means I don't have to run the __construct() manually via the internal "new self" method. Obviously, in this case, I probably don't even need to call the booter function __construct() but for relationship recognition I have. Basic idea ---------- Make the __construct() protected instead of private. This means an extended class can access/override it. Then, in autoload, I use eval to extend the class I've just initiated which simply runs the parent __construct() if it exists.. <?php function __autoload($class){ require_once(INC_FILES."/class/class.".$class.".php"); if(method_exists($class,'__construct')){ eval("class al_$class extends $class{function __construct(){parent::__construct();}}new al_$class;"); } return true; } ?> ---- Server IP: 92.48.74.199 Probable Submitter: 85.158.139.99 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.autoload.php Edit -- https://master.php.net/note/edit/89173 Del: integrated -- https://master.php.net/note/delete/89173/integrated Del: useless -- https://master.php.net/note/delete/89173/useless Del: bad code -- https://master.php.net/note/delete/89173/bad+code Del: spam -- https://master.php.net/note/delete/89173/spam Del: non-english -- https://master.php.net/note/delete/89173/non-english Del: in docs -- https://master.php.net/note/delete/89173/in+docs Del: other reasons-- https://master.php.net/note/delete/89173 Reject -- https://master.php.net/note/reject/89173 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#151097) next »