What is the difference between the two types of class constructors in PHP?

What is the difference between PHP classes when using the __construct constructor and when using the class name as a constructor?

For example:

 class Some { public function __construct($id) { .... } .... } 

OR

 class Some { public function Some($id) { .... } .... } 
+7
source share
1 answer

The top is a new way that it has been doing in PHP since version 5.0 and how all new code should be written. The latter is an old way of PHP 4 and is deprecated. At some point it will be completely out of date and generally removed from PHP.

Update

Starting with PHP 5.3.3 , methods with the same name as the last element of the class name with names will no longer be considered as a constructor. This change does not affect classes that do not contain names.

 <?php namespace Foo; class Bar { public function Bar() { // treated as constructor in PHP 5.3.0-5.3.2 // treated as regular method as of PHP 5.3.3 } } ?> 
+13
source

All Articles