PHP object-oriented -- constructor and Destructor

Source: Internet
Author: User
PHP object-oriented & mdash; constructor and Destructor. we need to execute some tasks when creating and destroying objects. For example, assign values to attributes when creating an object and disable data connections when the object is destroyed. in this case, constructors and Destructor are required.

In PHP, constructor and Destructor are fixed as follows:

// Construct ([argument1, argument2 ,..., argumentN]) {/* Class initialization code */} // destructor function _ destruct (){//...}

Constructor
  • Constructors can accept parameters and assign values to object attributes when creating objects.
  • Constructors can call class methods or other functions.
  • Constructors can call constructors of other classes.

Example of constructor:

 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 constructor and call the constructor of the irrelevant class:

Function _ construct () {parent ::__ construct (); // The constructor that calls the parent class must display the following :: __construct (); // call the constructors of other classes. classname is the class name // Other operations}

Inheritance and constructor

The constructor of the subclass in PHP does not take the initiative to call the constructor of the parent class. to be displayed, use parent ::__ construct () to call:

 SetName ($ name) echo "the animal category is created! ";}//... Other methods} class Birds extends Animal {private $ name; private $ leg; function _ construct ($ name, $ leg) {parent ::__ construct ($ name ); // display the call $ this-> setLeg ($ leg); echo "birds are created! ";}//... Other methods}?>

If multi-level inheritance is involved, when parent :__ construct () is called, it searches up along the parent class until the most suitable constructor is found, for example:

// Connection example class Parrot extends Birds {private $ name; private $ leg; private $ wing; function _ construct ($ name) {parent ::__ construct ($ name ); // at this time, no suitable constructor for the parent class (Birds class) is found. you can only search up and find the appropriate constructor echo "the parrot class is created! "; $ This-> smackTalk ();/* output:" The Animal class is created! "" Talking parrot! "*/} Function smackTalk () {echo" parrot speak! ";}}

If you want to call the constructor of several parent classes in sequence, you can use the class name to directly call the constructor. for example:

Function _ construct ($ name, $ leg) {Animal ::__ construct ($ name); // call the Animal constructor Birds :__ construct ($ name, $ leg); // call the Birds constructor}

PHP does not support function overloading. all constructors cannot have multiple.

Destructor
  • Destructor are called automatically when an object is destroyed and cannot be displayed.
  • The Destructor cannot contain parameters.

 

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

  • After the PHP page is loaded;
  • Unset () class;
  • When the variable reference points to another object or value;

The memory recovery mechanism of PHP is similar to that of JAVA. it destroys and recycles objects without any reference and uses the reference counter technology.

Example:

 ";?>

In this example, if three variables reference $ a, $ B, and $ c to the test object, the test object has three reference counts. when $ a = null, $ a's reference to the test object is lost, and the count-1 is changed to 2. when $ B is unset (), $ B's reference to the test object is also lost, after the count is-1, it becomes 1. after the page is loaded, the reference of $ c pointing to the test object is automatically released. at this time, the count-1 is changed to 0, and the test object has no variable reference, it will be destroyed and the destructor will be called.

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.