0

i'm trying to call a getter of an object dynamically (the getter is in a string) but it won't work:

$getterName = "get" . $name; //creation of the getter name like getFirstname
$value = call_user_func(array($player, $getterName()));

$player is an instance of myBundle\Entity\Player I'm running this code in a controller of another symfony bundle.

My error:

Uncaught PHP Exception Symfony\Component\Debug\Exception\UndefinedFunctionException: "Attempted to call function "getFirstname" from the global namespace."

I don't know how to solve this, does someone have an idea ?

Thanks

1 Answer 1

0

With $getterName() you're calling a global function: getFirstname. If you want call the getFirstname method of the myBundle\Entity\Player class, then you need to call it with:

$value = call_user_func(array($player, $getterName));
1
  • I tried and it worked. I didn't know that, thanks for your help Commented Dec 7, 2016 at 13:05

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.