Skip to content
Advertisement

PHP- difference between instantiating a class and returning the instantiation

The normal class declaration and instantiation:

JavaScript

The class declaration and instantiation with return:

JavaScript

What is the difference between these two in PHP in terms of using the classes?

Advertisement

Answer

If you just return new FooBar() it will just execute its __construct method if it has. But you can call methods without assigning objects to a variable. Like this:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement