2

I have a class like this

class someclass{
  public function somemethod(){}
}

Now I have an array:

$somearray['someclass']  = new someclass();
$somearray['somemethod'] = 'somemethod';

How can I fire them, I tried the following:

$somearray['someclass']->$somearray['somemethod']();

usign this I get the following error:

Fatal error: Method name must be a string in ......................

Anyone have an idea on how to do this?

4
  • 1
    Your syntax is correct and an error should not be thrown. Commented May 13, 2010 at 19:53
  • Strange, I have simplified the code here, so maybe my fault lies elsewhere. Commented May 13, 2010 at 19:55
  • When you run your own simplified code, do you still get the error? Commented May 13, 2010 at 20:01
  • The fault was elsewhere in the code. Everything is ok now and this is the right syntax. Thank you guys. Commented May 13, 2010 at 20:08

5 Answers 5

1

If it doesn't want to work that way (and I agree it should), you could try:

call_user_func(array($somearray['someclass'], $somearray['somemethod']));
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Nice method this works also. The code I used worked also, I had some other mistake in my code. But this is definitely a nice alternative.
0

I cannot reproduce the error with the code provided (As @webbiedave pointed out the syntax is correct).

However you could cast the method name to a string before using it to ensure the method name is a string. This will ensure that even it is a user defined object with a __toString() method, it will be converted into its string representation.

$somearray['someclass']->{(string)$somearray['somemethod']}();

3 Comments

I tried this, and I get another error:Call to a member function () on a non-object
@Saif you have an issue elsewhere in your code. This indicates that $somearray['someclass'] is not set or null. Also $somearray['somemethod'] is not set or is null.
Yes the fault was somewhere else. Everything is ok now even without the casting. Thanks for your time.
0

Try $somearray['someclass']->{$somearray['somemethod']}();

3 Comments

When I do this I get: Method name must be a string
Have you verified that $somearray['somemethod'] actually contains a string?
I've tested that this works. Just make sure that it is a string.
0

The following code was tested and seems to be want you want:

<?php


class someclass{
  public function somemethod(){ echo 'test'; }
}


$somearray['someclass']  = new someclass();
$somearray['somemethod'] = 'somemethod';

$somearray['someclass']->{$somearray['somemethod']}();

?>

Comments

0

How about this:

foreach (get_class_methods(get_class(new someclass()) as $method) {
    $method();    
}

Comments

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.