note 54191 added to language.oop5.autoload
| From: | christopher dot klein at ecw dot de | Date: | Mon, 27 Jun 2005 06:20:18 +0000 |
| Subject: | note 54191 added to language.oop5.autoload | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90988@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/language.oop5.autoload.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54191
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54191&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54191&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54191&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54191&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54191&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54191&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54191&report=yes
Search -- http://master.php.net/manage/user-notes.php