I have two objects. Object A and B.
A has a method which returns B. And I want to call this dynamically so I use a string for calling a method inside of B like so:
$method = 'getB()->someMethod';
But if do this:
$a = new A();
$a->$method();
It doesn't work. Any ideas?
Just use this: $str = "abcdefg". foo().
There are two methods to call a function from string stored in a variable. The first one is by using the window object method and the second one is by using eval() method. The eval() method is older and it is deprecated.
Method overloading ¶ __call() is triggered when invoking inaccessible methods in an object context. __callStatic() is triggered when invoking inaccessible methods in a static context. The $name argument is the name of the method being called.
You cannot do it like that. $method
can only contain the name of a method of A
. Read about variable functions. You could have to variables though, e.g.
$method1 = 'getB';
$method2 = 'someMethod';
$a->$method1()->$method2();
But probably it would be better to rethink the problem, consider another structure of your code and/or having a look at design patterns.
The question is: What is your ultimate goal?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With