Louis-Philippe Huberdeau <lp*********@sy mpatico.ca> wrote in message news:<qF******* ************@ne ws20.bellglobal .com>...
Create a factory-like method that will return an instance of the class
and use call_user_func( ) with an array as the first parameter
(containing the class name and your factory method)
class foo
{
function factory()
{
return new foo();
}
}
$obj = call_user_func( array('foo', 'factory') );
--
Louis-Philippe Huberdeau
Roger Vandervort wrote: I've leafed through the docs and user comments on php.net, but haven't
been able to find anything related to this.
Is there a way to dynamically instantiate a class, like you can in
Java with class.forName() ?
example:
$myclass = "Automobile ";
$func = "drive";
$myInstance = new $myclass;
call_user_metho d ($func, $myInstance); // i know this is deprecated.
Actually, I did determine that this does work:
class Automobile {
function myFunction($ech oString) { echo 'echoing
Automobile:'.$e choString; }
}
$myClass = "Automobile ";
$func = "myFunction ";
$param = 'Vroom...Vroom' ;
$myInstance = new $myClass;
call_user_funct ion(array(&$myC lass,$func),$pa ram); // will print
'echoing Automobile:Vroo m...Vroom';
Not sure which versions support this though.