PHP constructor and destructor

Source: Internet
Author: User

PHP constructor and destructor

<? Php

// Constructor PHP5 can use _ construct () in the class to define a constructor. classes with constructor will call this function each time an object is created,

// Initialization is often used when an object is created.

Class Person {

Public $ name;

Public $ age;

Public function _ construct ($ name, $ age)

{

$ This-> name = $ name;

$ This-> age = $ age;

Echo $ this-> name. "created! <Br/> ";

}

// Destructor The Destructor refers to the function that is executed when all references of an object are deleted or the object is explicitly destroyed.

Public function _ destruct ()

{

// Release resources

Echo $ this-> name. "destroyed! <Br/> ";

}

}

$ People = new Person ("", 20 );

Echo $ people-> name. "<br/> ";

$ People1 = new Person ("Xiaoqiang", 20 );

 

// Detailed explanation:

The destructor of the parent class will not be called by the engine secretly. To execute the destructor of the parent class, you must explicitly call parent ::__ destruct () in the destructor of the subclass (). In addition, just like constructors, if the subclass does not define the destructor, it will inherit the parent class.

The Destructor will be called even when the exit () Termination script is used. Calling exit () in the Destructor will stop other close operations.

Note:

The Destructor is called when the script is disabled. All HTTP header information is sent. The working directory when the script is disabled may be different from that in SAPI (such as apache.

Note:

An exception thrown in the Destructor (called when the script is terminated) will cause a fatal error.

Complete analysis:

The constructor and destructor first describe the differences between objects of the same class: first, the object names are different. Second, the data member values of the object are different. We can initialize an object when declaring it. If it is initialized, the program will apply to the operating system to allocate a certain amount of memory space for it. When it allocates memory space for it, the initialization will be written into the memory unit. The problem is that we cannot write the initial value into the memory unit during initialization as the initial variable, because compared with this initialization method, class objects are too complex.

Constructor: We use constructor to initialize class objects. C ++ calls constructor when creating an object. Constructor is a special class member function. Of course, you can directly aggregate all data members of the class. It can be an inline function, a list of parameters, a default value of parameters, or a reload.

We can assign the value of a variable to another variable of the same type. For an object of the class, we can also copy the content of an object to another object of the same class. The copy method can be to assign the values of the data variables of one object to the corresponding data variables of another object. However, if there are many data variables, This method may be a little troublesome. You can also use a special constructor to initialize another object with an existing object.

Copy constructor: A special constructor that is used to construct objects and has all the features of constructor. The copy constructor can use an existing object to initialize another object of the same class. The parameter used to copy the constructor is a reference to the object of this class. (Why must it be referenced ?)

The copy constructor is called in the following three cases: when one object of the class is used to initialize another object of the class, the system automatically calls the copy constructor to realize the copy assignment between objects; when the form parameter of a function is an object of A Class (can it be an object reference ?), When this function is called and the real parameter is assigned to the form parameter, the system automatically calls the copy constructor. When the return value of a function is an object of a class, the system calls the copy constructor. The last case is that the return value of the function func is A, and object A is A local variable in the function func. After the function func is executed, it is released. The statement for calling the function func is: B = func (). Object B is assigned A value in this way: When the compiler executes B = func (), A temporary unknown variable is created. When the func function executes the return A; statement, it actually calls the copy constructor to copy the value of object A to the temporary variable, and then releases the memory space occupied by object. Then, use the copy constructor to copy the value of the temporary variable to object B.

Destructor: All natural things have lifecycles, and class objects are no exception. If an object is declared in a function, the declared object will be released when the function execution ends and the function is called. The role of the Destructor is opposite to that of the constructor. The Destructor is automatically called when the object is to be deleted. After the Destructor is executed, the memory occupied by the object is released. If we want to clean up objects when they are deleted, we can write them in the destructor of this class.

PS: great originality: For details, please contact: 18825121252

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.