7

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.

2 Answers 2

20
$name::foo()

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

call_user_func(array($classname,$methodname))
Sign up to request clarification or add additional context in comments.

1 Comment

@KoolKabin: pass arguments as further arguments to call_user_func. There's also call_user_func_array. See php.net.
6

You can do something like this using the call_user_func function

it would look something like the following

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

Hope this helps

1 Comment

Yeah, that's the kind of stuff I'm looking for. Though I hoped it would be easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.