"Stijn Goris" <me*****@hotmail.com> wrote in message
news:q5*******************@phobos.telenet-ops.be...
hi all,
Is it possible to declare 2 constructors in the same class? The
constructors would have different number of parameters parameters...
kind regards
Stijn
This *probably* isn't what you're asking for but you *might* find it useful.
I have a requirement for chained constructors - when an object is
instantiated, I want a constructor-style function to run from each class
from which the final class inherits. PHP4 doesn't do that AFAIK. PHP5
might, I haven't looked. But anyway here's how I do it (no idea how this
source is going to look when posted, I expect it will be scrambled in
respect of indenting etc. - but should be clear enough hopefully).
class A
{
function A_autorun()
{
echo "Hello";
}
}
class B extends A
{
function B_autorun()
{
echo "Goodbye";
}
}
class Factory
{
function CreateObject()
{
$objNewObject = new B();
$arrMethods = get_class_methods($objApplicationObject);
foreach ($arrMethods as $strCurrentMethodName)
{
if (strpos($strCurrentMethodName,"autorun"))
{
$strToExecute = '$void =
$objApplicationObject->'.$strCurrentMethodName.'();';
eval($strToExecute);
}
}
}
}
With all that in place, in the main code I do this:
$objMyNewObject = Factory::CreateObject();
and receive this output:
HelloGoodbye
..
Hope this is useful to someone, perhaps even the OP. Obviously there may
well be better ways of doing this, and I guess it would make sense to try
and revise one's design so that this kind of thing wasn't necessary...
Cheers,
Matt Saunders.