PHP Object-oriented-constructors, destructors

Source: Internet
Author: User

Constructors and destructors are fixed in PHP, as follows:

//constructor function __construct ([Argument1,argument2,...,Argumentn]) {/    * Class Initialization code */  }//destructor function  __destruct () {    //...}
constructor Function
    • Constructors can accept parameters and be able to assign values to object properties when creating an object
    • Constructors can call class methods or other functions
    • Constructors can call constructors of other classes

Examples of constructor use:

<?phpclass person{    private $name;    Private $age;    Private $gender;    Public function __construct ($name, $age, $gender) {        $this->setname ($name);        $this->setage ($age);        $this->setgender ($gender);    }    Public Function SetName ($name) {        $this->name = $name;    }    ... getter setter method} $peron = new Person ("Lee", 18, ' Male '); >

Call the parent class constructor and call the constructor of the unrelated class:

function __construct () {    parent::__construct ();//The constructor that invokes the parent class must display the parent class constructor that is called by using parent    classname::__construct () ; Call constructors for other classes, ClassName is class name    //Other operation}
Inheritance and constructors

The constructor of a subclass in PHP does not actively invoke the constructor of the parent class, which is to be displayed using the Parent::__construct () Call:

<?phpclass animal{    private $name;    function __construct ($name) {        $this->setname ($name)        echo "Animal class was created! ";    }    // ... Other methods}class Birds extends animal{    private $name;    Private $leg;    function __construct ($name, $leg) {        parent::__construct ($name);//Display call        $this->setleg ($leg);        echo "Birds are created! ";    }    // ... Other methods}?>

If multiple inheritance is involved, when Parent::__construct () is called, it is searched up the parent class until the most appropriate constructor is found, for example:

Then the example class Parrot extends birds{    private $name;    Private $leg;    Private $wing;    function __construct ($name) {        parent::__construct ($name);//The appropriate constructor for the parent class (birds Class) is not found at this time, only the search is up and the animal class is searched. Just find the right constructor        echo "Parrot class was created! ";        $this->smacktalk ();        /* Output:        "Animal class was created!" "        the Parrot Speaks!" "        *    /} function Smacktalk () {        echo" Parrot talking! ";        }}

If you want to invoke the constructors of several parent classes in turn, you can call the constructors directly using the class name, for example:

function __construct ($name, $leg) {       animal::__construct ($name);//Call Animal constructor        birds::__construct ($name, $leg); Call Birds Constructor}

PHP is an overload that does not support functions, and all constructors cannot have more than one.

Destructors
    • Destructors are calls that are automatically called when an object is destroyed and cannot be displayed.
    • Destructors cannot take parameters.

Destructors may be called (but not necessarily) in the following situations:

    • After the PHP page has finished loading;
    • Unset () class;
    • When a variable reference points to another object or value;

PHP's memory recovery mechanism is similar to that of Java, where no reference objects are destroyed and recycled, using a reference counter technique.

Example:

<?phpclass test{    function __destruct () {        echo] is called when the object is destroyed!!! ";    }} $a = $b = $c = new test (); $a = Null;unset ($b); echo "

In this case, for example, there are three variables that reference $ A, $b, $c point to the test object, the test object has 3 reference counts, and when $ A = NULL, $a the reference to the test object is lost, the count-1, becomes 2, and when $b is unset (), $b reference to the test object is lost, The count is 1, becomes 1, the last page is loaded, the reference to the test object is automatically freed, the count is then 1, and the 0,test object is destroyed without a variable reference, and the destructor is called.

PHP Object-oriented-constructors, destructors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.