Skip to content
Advertisement

Tag: oop

PHP- difference between instantiating a class and returning the instantiation

The normal class declaration and instantiation: The class declaration and instantiation with return: What is the difference between these two in PHP in terms of using the classes? 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:

How to specify the void return type for a constructor

For consistency I’m specifying return types since PHP 7.1, for all methods, including magic ones like __toString, and even when the implicit return type is void like with __unserialize(): When I try the same for constructors and destructors, like this: PHP yields Fatal errors: The only thing I can do right now is to specify the implicit return type in

Method to change specific value inside multidimensional array

Given a class with a multidimensional array as private property. I’m trying to write a method setValue() which could change any value of this private array by passing: some kind of path to a specific value (e.g. $path = [‘lorem’ => [‘ipsum’ => [‘dolor’ => [‘sit’ => null]]]];) the new value … Thanks to a comment of Gabriel on php.net

Advertisement