How do I dynamically invoke a class method in PHP?

mmattax picture mmattax · Nov 7, 2008 · Viewed 62.6k times · Source

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.

Answer

Peter Bailey picture Peter Bailey · Nov 7, 2008

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)