php中构造函数和统一构造函数之间的区别

Is there any difference in constructor or unified constructor. I have a class which have a constructor and a unified constructor. When i intialise object of the class then unified constructor call why not normal constructor.

<?php 

class test {

    function __construct() {
       print "In BaseClass constructor
";
    }

    function test() {
        echo 'Class loeaded';                   
    }
}

$obj = new test();

?>

OUTPUI

In BaseClass constructor

PHP Version 5.2.6

Since PHP 5, the best way to declare a constructor method in a class is to use the standardized

__construct()

So if you have

class myA {

  public function __construct() {
    echo "hello construct myA";
  }

  public function myA() {
    echo "hello basic way myA";
  }
}

$myA = new myA();

It will echo

hello construct myA

because the __constructor() has priority

But, for compatibility reason, the old way may work if there is not __construct() method. And:

class myA {

  //public function __construct() {
  //  echo "hello construct A";
  //}

  public function myA() {
    echo "hello basic way myA";
  }
}

$myA = new myA();

will give you:

hello basic way myA

In all cases, I advise you to use __construct()

the old constructor (method with class name) is only there for compatibility reasons. so if you have the new constructor (__construct) in the class php won't bother to call the old one.

edit

interesting note is that calling parent::__construct when the class being extended has only the old constructor type will still work (it becomes like a alias to the real constructor)

Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.

Read documentation of constructor (link)


Instances of classes are created using the new keyword. What happens during the new call is that a new object is allocated with its own copies of the properties defined in the class you requested, and then the constructor of the object is called in case one was defined. The constructor is a method named __construct(), which is automatically called by the new keyword after creating the object. It is usually used to automatically perform various initializations such as property initializations. Constructors can also accept arguments, in which case, when the new statement is written, you also need to send the constructor the function parameters in between the parentheses.

In PHP 4, instead of using __construct() as the constructor’s name, you had to define a method with the classes’ names, like C++. This still works with PHP 5, but you should use the new unified constructor naming convention for new applications.

We could pass the names of the people on the new line in the following class example :

class Person {

function __construct($name)

{

$this->name = $name;

}

function getName()

{

return $this->name;

}

private $name;

};

$judy = new Person("Judy") . "
";

$joe = new Person("Joe") . "
";

print $judy->getName();

print $joe->getName();

Tip: Because a constructor cannot return a value, the most common practice for raising an error from within the constructor is by throwing an exception.

According to documentation As of PHP 5.3.3, methods with the same name as that of class will no longer be treated as constructor .. They are like regular methods ...

 <?php

 class myclass 
  {
     public function myclass() 
    {
    // treated as constructor in PHP 5.3.0-5.3.2
    // treated as regular method as of PHP 5.3.3
    }
   }
     ?>

`

For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, and the class did not inherit one from a parent class, it will search for the old-style constructor function, by the name of the class.

It means that there if you have constructor myclass() and __construct .. then __construct will be searched for first and taken as constructor instead of myclass()

         class myclass 
          {
             public function myclass() 
            {
               echo 'hello';
            }

            function __construct() {
               print "new constructor"
           }

           }
     $obj = new myclass();  // will echo new constructor