Re: Re: dynamic object instances

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Eli wrote:
Thomas Angst wrote:
 > Thanks for you answer, but sorry, I do not understand your hint. I tried
 > this code:
 > class test {
 >    var $txt;
 >    function test($txt) { $this->txt = $txt; }
 >    function out() { echo $this->txt; }
 > }
 > $obj = call_user_func_array(array('test', 'test'), array('foobar'));
 > $obj->out();
 > But I'm getting an error while accessing the $this pointer in the
 > constructor.

This will not work, since it is like calling a static class method, and $this is not allowed to be used in static methods.

I solved this with eval() function.
<?
$obj_eval="return new $class(";
for ($i=0; $i<count($args); $i++)
   $obj_eval.="\$args[$i],";
$obj_eval=substr($obj_eval,0,-1).");";
$obj=eval($obj_eval);
?>

I believe that this is the kind of clever, evil stuff the OP was trying to avoid...
(evil - eval :-) - besides eval is very slow - not something you (well me then) want to
use in a function dedicated to object creation which is comparatively slow anyway
(try comparing the speed of cloning and creating objects in php5 for instance

regardless - nice one for posting this Eli - I recommend anyone who doesn't understand
what he wrote to go and figure it out, good learning material :-)



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux