tags:

views:

741

answers:

2

Is there a way to call Static Classes / Methods by name?

Example:

$name = 'StaticClass';
($name)::foo();

I have classes which I keep all static methods in and I'd like to call them this way.

+3  A: 

Hi,

You can do something like this using the call_user_func function

it would look something like the following

$name = 'staticClass';
user_call_func(array($name, 'foo'));

Hope this helps

Anthony
Yeah, that's the kind of stuff I'm looking for. Though I hoped it would be easier.
smack0007
+8  A: 
$name::foo()

is possible since PHP5.3. In earlier versions you have to use

call_user_func(array($classname,$methodname))
porneL
what if we wanna pass value too...
KoolKabin
@KoolKabin: pass arguments as further arguments to `call_user_func`. There's also `call_user_func_array`. See php.net.
porneL