note 40300 added to function.eval

From: Date: Fri, 27 Feb 2004 21:46:11 +0000
Subject: note 40300 added to function.eval
Groups: php.notes 
Request: Send a blank email to php-notes+get-65908@lists.php.net to get a copy of this message
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. ?> ---- Manual Page -- http://www.php.net/manual/en/function.eval.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40300 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40300&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40300&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#65908) next »