Re: OO method inside a variable

From: Date: Wed, 04 Nov 2009 21:46:57 +0000
Subject: Re: OO method inside a variable
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-299624@lists.php.net to get a copy of this message
simone.nanni@ptvonline.it wrote:
Hi everybody, i'm trying to apply a method to an object getting its name from a variable, that i obtain parsing an XML file. For example: $object = new Class; $method = "row()"; #I'm getting this from the XML parser $object->$method; #I've an error here... Obviously the method inside the class exists. How can i do it? Thanks a lot in advance.
Your problem is the inclusion of parameters in the $method. You can do the following: <?php
    $object = new Class;
    $method = 'row';
    $ret = $object->$method();
?> Or if you insist on embedded parameters (but this is a security risk): <?php
    $object = new Class;
    $method = 'row()';
    $ret = eval( "return \$object->$method;" );
?> Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP

« previous php.general (#299624) next »