Answer the question
In order to leave comments, you need to log in
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();
$widgetHeadMenu = (new Widgets)->headMenu();
call_user_func([new Widgets, "headMenu" ] );
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question