note 54191 deleted from language.oop5.autoload by felipe

From: Date: Mon, 24 Mar 2008 22:32:04 +0000
Subject: note 54191 deleted from language.oop5.autoload by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-137488@lists.php.net to get a copy of this message
Note Submitter: christopher dot klein at ecw dot de ---- Sometimes you could get in trouble, if you have already written a management-class for an object, but you still don not have designed the object. For example you have created a class called Management_FileObject: <?php class Management_FileObject { private static $objFile; public static function getInstance() { if (!is_object(self::$objFile)) { self::$objFile = new FileObject(); } return self::$objFile; } public static function writeData($data) { self::getInstance()->setData($data); } public static function readData() { return self::getInstance()->getData(); } } ?> The object FileObject is still not defined (why-ever). Now we can use a method, which I want to name "Extreme Prototyping". Redefine the __autoload()-function: <?php function __autoload($className) { // Define more search-pathes here $arrPath = array('../classes/'); for ($i = 0, $m = sizeof($arrPath); $i < $m; $i++) { $phpFile = $arrPath[$i].$className.".php5"; if (file_exists($phpFile)) { include_once($phpFile); return true; } } $str = 'class '.$className.' { private static $objInstances = array(); public function __construct() { self::$objInstances[] = $this; } private $objVals = array(); public function __call($m, $a) { if (preg_match("/^(set|get)(.*)/",$m,$arrRet)) { if ($arrRet[1] == "get") { if ($this->objVals[$arrRet[2]]) { return $this->objVals[$arrRet[2]]; } } else { $this->objVals[$arrRet[2]] = $a[0]; return true; } } } public function dumpVar() { var_dump($this); } public static function dumpObjects() { var_dump(self::$objInstances); } }'; eval($str); } ?> Now you can test the code above with this lines: <?php $testData = "Extreme Prototyping success"; Management_FileObject::writeData($testData); if ($testData != ($returnData = Management_FileObject::readData())) { print "Extreme Prototyping falied."; } else { print "Got ".$returnData; } ?> I hope, you find this usefull. Best regards, Christopher Klein

« previous php.notes (#137488) next »