A
A
AirBook2019-09-12 11:27:49
Zend Framework
AirBook, 2019-09-12 11:27:49

How else can you substitute a method call?

$res = $this->$method['name']($parser_name);
Question 1 I came across this code, is it normal from the point of view of OOP?
serginhold fully answered this question. I knew that the code for substituting the method, but I thought why do it, it turns out you don’t need to do it

$method = 'getName';
$obj->$method(); // $obj->getName();

$method = 'getTitle';
$obj->$method(); // $obj->getTitle();

Question 2 Is there a better way to do the same?
I think it's better to rewrite the program logic so that such things do not need to be used at all, but I wonder if you still need to use such a "method auto-substitution" correctly?
The answer of hack504 seems to me the most correct from the point of view of OOP, I'll go and read about reflection
$this->{$method['name']}($parser_name);
//можно и через рефлексию: 
$reflectionMethod = new ReflectionMethod($this, $method['name']);
$reflectionMethod->invoke($this, $parser_name);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
serginhold, 2019-09-12
@AirBook

nothing, usually it looks like shit code, because situations where this is needed are extremely rare
sense in passing the name of a method or property through a variable, and that's it. Type in one case such a method, in another another

$method = 'getName';
$obj->$method(); // $obj->getName();

$method = 'getTitle';
$obj->$method(); // $obj->getTitle();

ps the question is being edited on the go .. if the author asked "what is the point of doing this"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question