Re: Calling a static method with call_user_method?

From: Date: Sun, 18 May 2003 15:47:49 +0000
Subject: Re: Calling a static method with call_user_method?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-148091@lists.php.net to get a copy of this message
On Sun, 18 May 2003 15:08:36 +0200, you wrote: >Is it possible to call a static method from a object with call_user_func? >Cause call_user_method needs an instance of an object as parameter, you >can't pass a name of a class in a string. You sound like a Java programmer :) My first reaction was that this isn't possible, but I messed about for ten minutes and came up with this: <? /* Class A has method B */ class A { function B ($s = "None") { echo ("<p>input : $s</p>"); } } /* C is an instance of A */ $C = new A (); /* invoke A::B */ A::B ("call 1"); /* invoke C->B */ $C->B ("call 2"); /* invoke A::B via call_user_func() */ call_user_func (array("A", "B"), "call 3"); /* invoke C->B via call_user_func() */ call_user_func (array($C, "B"), "call 4"); /* invoke A::B via call_user_func_array() */ call_user_func_array (array("A", "B"), array("call 5")); /* invoke C->B via call_user_func_array() */ call_user_func_array (array($C, "B"), array("call 6")); ?> Which seems to cover every possible variation. I think it's pretty nasty unless you have a very good reason, but it's your code :) Let me know if it works for you.

« previous php.general (#148091) next »