11

The following code works fine.

LibraryTests::TestGetServer();

Get the array of functions in LibraryTests and run them:

$methods = get_class_methods('LibraryTests');
foreach ($methods as $method) {
  call_user_func('LibraryTests::' . $method . '()' );
}

This throws an error: Warning: call_user_func(LibraryTests::TestGetServer()) [function.call-user-func]: First argument is expected to be a valid callback

Here is the class that is being called:

class LibraryTests extends TestUnit {

    function TestGetServer() {
        TestUnit::AssertEqual(GetServer(), "localhost/");
    }
    .
    .
    .

How to fix?

Working in PHP 5.2.8.

1
  • Do not append () if the function has no parameters Commented Mar 2, 2015 at 10:52

1 Answer 1

16

Either (as of PHP 5.2.3):

$methods = get_class_methods('LibraryTests');
foreach ($methods as $method) {
  call_user_func('LibraryTests::' . $method);
}

Or (earlier):

$methods = get_class_methods('LibraryTests');
foreach ($methods as $method) {
  call_user_func(array('LibraryTests', $method));
}

See call_user_func­Docs and the Callback Pseudo-Type­Docs.

Sign up to request clarification or add additional context in comments.

4 Comments

Well you need to have a valid callback for that to work. If you put invalid values into $method, it does not work. If you put valid values in there, it does work. Take care that this is for static class methods only.
Both throw ... [function.call-user-func]: First argument is expected to be a valid callback in ...
Well, first of all define the mehtod as static in your class: static function TestGetServer - or work on a class instance with the array notation. I linked two pages from the manual which contain a lot of information and examples and should lead you the way so that you can solve the problem in whichever way suits your needs.
OK, that worked. It needs static before function TestGetServer() in class LibraryTests. Thanks.

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.