How do I dynamically invoke a class method in PHP?

Issue

How can I dynamically invoke a class method in PHP? The class method is not static. It appears that

call_user_func(...)

only works with static functions?

Thanks.

Solution

It works both ways – you need to use the right syntax

//  Non static call
call_user_func( array( $obj, 'method' ) );

//  Static calls
call_user_func( array( 'ClassName', 'method' ) );
call_user_func( 'ClassName::method' ); // (As of PHP 5.2.3)

Answered By – Peter Bailey

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published