D
D
dev4002016-05-16 14:25:54
PHP
dev400, 2016-05-16 14:25:54

Cons and pros of each of the options for working with an object?

What are the pros and cons of each option?
First

$widgets = new Widgets();
$widgets->headMenu();

Second
$widgetHeadMenu = (new Widgets)->headMenu();
Third
call_user_func([new Widgets, "headMenu" ] );

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nikolaev, 2016-05-16
@dev400

The first and second options are essentially synonyms, only the second is an abbreviated notation of the first. It can be used if you do not need to pass parameters to the class constructor. If necessary, it is better to use the first one, for reasons of readability of the code. The difference in speed between them is negligible.
The third option is not to call objects like this. In 95% of cases, the developer does not need it at all, besides, in this case, the function is called once again. For developing frameworks \ CMS, it is more than useful, because. allows you to operate on variables with the names of objects, classes, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question