I'm confused as your first post says dynamic constructor invocation with arbitrary number of arguments
And you third post says they don't take unknown number of arguments...
So I assume what you mean is that the class ctor() "definitions" are fixed, but because the class can be *any* class the parameter list is therefore arbitrary???
Edit I've re-read your post a few times and it seems you solved the problem already, but I am missing something...
Why can't you call the ctor() normally via *new* operator as you've descirbed and pass it the variable number of arguments?
Code: Select all
$args = array('param1', '$param2', 213553);
$obj = new $myClass($args);
// This creates the object, returns the instance and
// passes the ctor() an arbitrary number of parameters
// depending on your situation???
//
// What am I missing?
//
// To my knowledge you can use a normal class as below
// with a fixed argument list, avoiding use of func_get_args(), etc...
What you have done or show in example won't work, because $
className is just that...a class...so when you use
call_user_func and pass it
__construct the script will choke as ctor()/dtor() are are not *true* member functions as they can never return values and cannot be made static, etc...
They are special functions used by the host language object model framework...
You should not ever have to call ctor()/dtor() directly, but technically you can although ctor() makes no sense, as I've said, they cannot return values...so calling ctor() does nothing and I guess compilers might even complain as you cannot initialize an object which doesn't exist...
It is the
new operator which returns the object instance not the ctor().
if $
className was an object instead, the code works fine as you are now calling an objects method, but you still cannot call the ctor() this way or at least it serves no purpose as the object is already created...
So you now understand why either class or object is being used *you cannot call a ctor() and expect proper results* even if it's allowed by host language object model...
What you need to do is rely solely on the natural object creation facilities of PHP and do something like:
Code: Select all
include(dirname(__FILE__).'/class.test.php');
$className = 'MyClass';
$obj = new $className('fname', 'lname');
You already hinted at this and it's the way to go...if I understand you correctly anyways
$
className is now dynamic and can be anything your heart desires...and you instantiate the object as normal...like calling a function dynamically - but in this case an special kind of function...
Now you can use the functions
arborint suggested to get the list of arguments, or you can hardcode them as I expect you want to, like so:
Code: Select all
class MyClass{
function __construct($fname, $lname)
{
echo 'construct() called<br />';
echo $fname.'<br />';
echo $lname.'<br />';
}
function __destruct()
{
echo 'destruct() called<br />';
}
}
I'm truley confused by what you want now as the above works, I've tried it...
Allowing both arbitrary arguments and any class you desire...
Cheers
