Derived constructor names obsolete in PHP?

PHP currently supports two naming conventions for constructors. PHP 4 supports Java-style constructors:

class A { public function A() { echo "I'm a constructor for class A!"; } } 

PHP 5 supports both Java-style constructors and the magic method syntax:

 class A { public function __construct() { echo "I'm a constructor for class A!"; } } 

The Java-style syntax is deprecated, and some of its functions do not work in the latest PHP . However, this has an interesting property, which I do not know an analogue for the syntax of the "magic method". If the foobar derived class does not have an explicit constructor, then the foobar method of the base class becomes the foobar constructor:

 class A { public function A() { echo "I'm a constructor for class A!"; } public function B() { echo "I'm a constructor for class B!"; } } class B extends A { } 

Since Java style constructors are becoming obsolete, what will happen to the set-the-constructor-in-parent language function?

+6
source share
2 answers

You probably found out about it here for PHP 4.

As you already know, the PHP 5 documentation ( here ) states that if __construct () is not used or inherited, the old syntax works like a reserve:

For backward compatibility, if PHP 5 cannot find the __construct () function for this class, and the class does not inherit it from the parent class, it will look for the old-style constructor function by the name of the class. In fact, this means that the only case that would have compatibility issues is that the class had a method called __construct () that was used for different semantics.

Thus, the old syntax must be valid, as is PHP 5.4.11. There is currently no explicit plan for obsolescence, even if it is predictable.

In any case, when (and if) they are deleted, this function will also be discarded, since there is no way to reproduce it using the __construct () syntax.

+2
source

What version of PHP are you currently working in?

If you have PHP 5 installed:

If you are creating new classes, use the new style. If you already have classes with the previous style, they still work, but it's good that you update them when you can.

If you have installed PHP 4:

Yopu should use the previous style. If you upgrade to PHP 5 later, they also work.

Greetings.

0
source

All Articles