Php constructor and Destructor php memory management function

Source: Internet
Author: User
Php constructor and Destructor php memory management function

  1. Class person {
  2. Var $ name;
  3. Var $ age;
  4. Function _ construct () {// Constructor
  5. $ This-> name = "lisi ";
  6. $ This-> age = 28;
  7. }
  8. Function say (){
  9. Echo "my name is". $ this-> name ."
    ";
  10. Echo "my age is". $ this-> age ."

    ";

  11. }
  12. }
  13. $ Per = new person ();
  14. $ Per-> say ();
  15. $ Per-> name = "zhangsan ";
  16. $ Per-> age = 26;
  17. $ Per-> say ();
  18. ?>

The constructor is opposite to the constructor. The Destructor is newly added to php5 and does not contain the Destructor in php4. Destructor allow you to perform some operations or complete some functions before a class is destroyed. these operations or functions are usually automatically generated when all references to this class are reset or out of scope. Similar to the constructor name, the destructor name of a class must be _ destruct (). The Destructor cannot contain any parameters.

  1. Class person {
  2. Function _ destruct ()
  3. {
  4. Echo "bye! ";
  5. }
  6. }
  7. $ A = new person ();
  8. ?>

If you declare a function in a class and name it _ construct, this function will be treated as a constructor and executed when an object instance is created. Clearly, __is two underscores. Like any other function, constructor may have parameters or default values. You can define a class to create an object and put all its attributes in a statement.

You can also define a function named _ destruct. php will call this function before the object is destroyed. It is called a php destructor.

Inheritance is a powerful function of a class. A class (subclass/derived class) can inherit the functions of another class (parent class/base class. The derived class contains all the attributes and methods of the base class, and you can add other attributes and methods to the derived class. You can also override the methods and attributes of the base class. Like 3. 1. As shown in 2, you can use the extends keyword to inherit a class.

You may want to know how constructors are inherited. When they are inherited together with other methods, they are not executed when the object is created. If you need this function, you need to use: operator. It allows you to point to a namespace. Parent points to the namespace of the parent class. you can use parent ::__ construct to call the constructor of the parent class.

Some object-oriented languages name constructors after the class. The same is true for the first few versions of php. this method is still valid. That is: if you name a class animal and create a method named animal in it, this method is the constructor. If a class has both the _ construt constructor and the same class name, php regards _ construct as the constructor. This makes the class written in the previous php version still usable. But the new script (php5) should use _ construct.

Php's new method of declaring constructor can make the constructor have a unique name, no matter what the name of its class is. In this way, you do not need to change the name of the constructor when changing the class name.

You may give the constructor the same access method as other class methods in php. The access method will affect the ability to instantiate objects within a certain range. This allows some fixed design patterns, such as the singleton mode.

Php destructor, opposite to Constructor. Php calls them to destroy an object from the memory. By default, php only releases the memory occupied by object properties and destroys object-related resources. The Destructor allows you to execute arbitrary code to clear the memory after using an object.

When php decides that your script is no longer related to objects, php destructor will be called. In the namespace of a function, this occurs when the function returns. For global variables, this occurs at the end of the script. If you want to explicitly destroy an object, you can assign any other value to the variable pointing to the object. Usually assign a value to null or call unset.

The following example calculates the number of objects instantiated from the class. Counter class value-added from constructor and impairment of the php destructor.

Once you define a class, you can use new to create an instance of this class. A class is defined as a design drawing, while an instance is a component placed on an assembly line. New requires the class name and returns an instance of the class. If the constructor requires a parameter, you should enter the parameter after new. Example:

  1. Class counter {
  2. Private static $ count = 0;
  3. Function _ construct () {self: $ count ++ ;}
  4. Function _ destruct () {self: $ count --;}
  5. Function getcount () {return self: $ count ;}}
  6. // Create the first instance and call the constructor. $ count = 1
  7. $ C = new counter ();
  8. // Output 1
  9. Print ($ c-> getcount ()."
    ");
  10. // Create the second instance and call the constructor. $ count = 2
  11. $ C2 = new counter ();
  12. // Output 2
  13. Print ($ c-> getcount ()."
    ");
  14. // Destroy the instance and call the destructor. $ count = 1
  15. $ C2 = null;
  16. // Output 1
  17. Print ($ c-> getcount ()."
    ");
  18. ?>

Program running result: 121 when a new php destructor instance is created, the memory will be prepared to store all attributes. Each instance has its own unique set of attributes. However, the method is shared by all instances of this class.

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.