The normal class declaration and instantiation:
JavaScript
x
class Foo {
// properties
// methods
}
new Foo();
The class declaration and instantiation with return:
JavaScript
class FooBar {
// properties
// methods
}
return new FooBar();
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
return (new FooBar())->yourMethod();