Php object-oriented full strategy (IV) constructor and constructor

Source: Internet
Author: User
Most classes have a special method called constructor. When an object is created, it automatically calls the constructor, that is, when the new keyword is used to instantiate the object, the constructor is automatically called. 8. constructor and constructor
Most classes have a special method called constructor. When an object is created, it automatically calls the constructor.
Number, that is, the constructor is automatically called when the new keyword is used to instantiate an object.
The constructor declaration is the same as that of other operations, but its name must be _ construct (). This is in PHP5.
In previous versions, the constructor name must be the same as the class name, which can still be used in PHP5,
Currently, it is rarely used. the advantage of doing so is that the constructor can be independent of the class name. when the class name changes
You need to change the corresponding constructor name. For backward compatibility, if a class does not contain the _ construct () method,
PHP will search for a method in php4 with the same name as the class name.
Format: function _ construct ([parameter]) {...}
Only one constructor can be declared in a class. Instead, the constructor is called only once each time an object is created.
Creation method. you cannot call this method proactively. Therefore, it is usually used to execute some useful initialization tasks. For example
Grant the initial value when creating an object.
Code snippet
The code is as follows:
// Create a human
Class Person {
// The following are the member attributes of a person.
Var $ name; // The name of a person.
Var $ sex; // gender of a person
Var $ age; // age of a person
// Define a constructor parameter: name $ name, gender $ sex, and age $ age
Function _ construct ($ name, $ sex, $ age ){
// Assign the initial value to the member attribute $ this-> name through the $ name passed in by the constructor
$ This-> name = $ name;
// The $ sex passed in through the constructor is used to assign the initial enable value to the member attribute $ this-> sex.
$ This-> sex = $ sex;
// Assign the initial value to the member attribute $ this-> age through the $ age passed in by the constructor
$ This-> age = $ age;
}
// How this person speaks
Function say (){
Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";
}
}
// Create three objects $ p1, p2, and $ p3 by using the constructor, and input three different real parameters: name, gender, and age.
$ P1 = new Person ("zhang san", "male", 20 );
$ P2 = new Person ("Li Si", "female", 30 );
$ P3 = new Person ("Wang Wu", "male", 40 );
// The following method of accessing the $ p1 object
$ P1-> say ();
// The following method of accessing the $ p2 object
$ P2-> say ();
// The following method is used to access the $ p3 object.
$ P3-> say ();
?>

Output result:
My name is Zhang San Gender: Male my age is: 20
My name is Li Si Gender: Female my age is: 30
My name is: Wang Wu Gender: Male my age is: 40

= 700) window. open ('/upload/20090930220209825.gif'); "src =" http://files.bitsCN.com/upload/20090930220209825.gif "onload =" if (this. width> '200') this. width = '000000'; if (this. height> '20140901') this. height = '000000'; "border = 0>
Destructor:
The constructor is opposite to the constructor. The Destructor is a newly added content of PHP5, which is not analyzed in PHP4.
Constructor. The Destructor allows you to perform some operations or complete some functions before destroying a class, such as closing a file,
Release result sets, etc. The destructor will be executed when all references to an object are deleted or when the object is explicitly destroyed,
That is, the destructor is called before the object is destroyed in the memory. Similar to the constructor name, the destructor name of a class
It must be _ destruct (). The Destructor cannot contain any parameters.
Format: function _ destruct (){......}
Code snippet
The code is as follows:
// Create a human
Class Person {
// The following are the member attributes of a person.
Var $ name; // The name of a person.
Var $ sex; // gender of a person
Var $ age; // age of a person
// Define a constructor parameter: name $ name, gender $ sex, and age $ age
Function _ construct ($ name, $ sex, $ age ){
// Assign the initial value to the member attribute $ this-> name through the $ name passed in by the constructor
$ This-> name = $ name;
// The $ sex passed in through the constructor is used to assign the initial enable value to the member attribute $ this-> sex.
$ This-> sex = $ sex;
// Assign the initial value to the member attribute $ this-> age through the $ age passed in by the constructor
$ This-> age = $ age;
}
// How this person speaks
Function say (){
Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";
}
// This is a destructor called before the object is destroyed.
Function _ destruct (){
Echo "goodbye". $ this-> name ."
";
}
// Create three objects $ p1, p2, and $ p3 by using the constructor, and input three different real parameters: name, gender, and age.
$ P1 = new Person ("zhang san", "male", 20 );
$ P2 = new Person ("Li Si", "female", 30 );
$ P3 = new Person ("Wang Wu", "male", 40 );
// The following method of accessing the $ p1 object
$ P1-> say ();
// The following method of accessing the $ p2 object
$ P2-> say ();
// The following method is used to access the $ p3 object.
$ P3-> say ();
?>

Output result:
My name is Zhang San Gender: Male my age is: 20
My name is Li Si Gender: Female my age is: 30
My name is: Wang Wu Gender: Male my age is: 40
Goodbye, James
Goodbye, Li Si
Goodbye Wang Wu

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.