note 40300 deleted from function.eval by sniper
| From: | sniper@php.net | Date: | Thu, 29 Dec 2005 09:15:30 +0000 |
| Subject: | note 40300 deleted from function.eval by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-100913@lists.php.net to get a copy of this message | ||
Note Submitter: yboily at seccuris dot com
----
When working with classes I was attempting to make a factory class for objects. My objective was to
create a class which would create objects based on values the factory was created with.
I attempted to create the objects by calling $newObject = new $$class;
In the long run this was used to generate security settings.
<?php
class clsObject
{
var $value;
function getValue() { return $this->value; }
}
class clsSuperObject extends clsObject{
....
}
class clsFactory
{
var $baseValue;
function clsFactory($val) {$this->baseValue = $val; }
function createObject($object) {
// $newObj = new $$object; will not work.
$newObj = eval(sprintf(" return new %s();", $objType));
$newObj->value = $this->baseValue;
return $newObj;
}
}
$myFactory = new clsFactory(10);
$myObject = $myFactory->createObject("SuperObject");
$myObject2 = $myFactory->createObject("Object");
and so on ;)
I found this extremely useful.
?>