8. Constructors and destructors
Most classes have a special method called a constructor. When an object is created, it will automatically call the constructor
number, that is, the constructor method will be automatically called when the new keyword is used to instantiate the object.
The declaration of a constructor is the same as the declaration of other operations, except that its name must be __construct(). This is a
change in PHP5. In previous versions, the name of the constructor must be the same as the class name. This can still be used in PHP5, but
is rarely used by people now. This is how The advantage is that the constructor can be made independent of the class name. When the class name changes, there is no need to change the corresponding constructor name. For backward compatibility, if there is no method named __construct() in a class,
PHP will search for a constructor method written in php4 with the same name as the class name.
Format: function __construct ([parameter]) { ... ... }
Only one constructor can be declared in a class, but the constructor will only be called once every time an object is created
Creation method, this method cannot be called actively, so it is usually used to perform some useful initialization tasks. For example, the paired attribute
is assigned an initial value when creating the object.
Code snippet
Copy code The code is as follows:
//Create a human
class Person{
//The following are the member attributes of the person
var $name; //The person’s name
var $sex; //The person’s gender
var $age; //The person’s Age
//Define a constructor parameter as name $name, gender $sex and age $age
function __construct($name, $sex, $age){
//Passed in through the constructor $name assigns an initial value to the member attribute $this->name
$this->name=$name;
//The $sex passed in through the constructor method is assigned to the member attribute $this->sex Assign the initial value
$this->sex=$sex;
//The $age passed in through the constructor method assigns the initial value to the member attribute $this->age
$this-> ;age=$age;
}
//How this person speaks
function say(){
echo "My name is: ".$this->name." Gender: ".$this->sex." My age is: ".$this->age."
";
}
}
//Create 3 through the constructor method Objects $p1, p2 and $p3 respectively pass in three different actual parameters: name, gender and age
$p1=new Person("张三","男", 20);
$p2 =new Person("李四","女", 30);
$p3=new Person("王五","male", 40);
//The following accesses the words in the $p1 object Method
$p1->say();
//The following accesses the speaking method in the $p2 object
$p2->say();
//The following accesses the $p3 object How to speak
$p3->say();
?>
The output result is:
My name is: Zhang San Gender: Male My age Yes: 20
My name is: Li Si Gender: Female My age is: 30
My name is: Wang Wu Gender: Male My age is: 40
As shown in the picture:
=700) window.open('/upload/20090930220209825.gif');" src="http://www.bkjia.com/uploads/allimg/131016/0921001394-0.gif" onload="if (this.width>'700')this.width='700';if(this.height>'700')this.height='700';" border=0>
The code is as follows:
//Create a human
class Person{
//The following are the member attributes of the person
var $name; //The name of the person
var $sex; //Person’s gender
var $age; //Person’s age
//Define a constructor parameter as name $name, gender $sex and age $age
function __construct($name , $sex, $age){
//The $name passed in through the constructor is assigned an initial value to the member attribute $this->name
$this->name=$name;
//The $sex passed in through the construction method is assigned an initial value to the member attribute $this->sex
$this->sex=$sex;
//The $age passed in through the construction method is assigned an initial value The member attribute $this->age is assigned an initial value
$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, which is called before the object is destroyed
function __destruct(){
echo "Goodbye".$this->name."
" ;
}
//Create three objects $p1, p2, $p3 through the construction method, and pass in three different actual parameters: name, gender and age
$p1=new Person(" "Zhang San", "Male", 20);
$p2=new Person("李思", "Female", 30);
$p3=new Person("王五", "Male", 40);
//The following accesses the speaking method in the $p1 object
$p1->say();
//The following accesses the speaking method in the $p2 object
$p2-> ;say();
//The following accesses the speaking method in the $p3 object
$p3->say();
?>
The output result is:
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 Zhang San
Goodbye Li Si
Goodbye Wang Wu
http://www.bkjia.com/PHPjc/320651.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320651.htmlTechArticle 8. Constructor and destructor methods Most classes have a special method called a constructor. When an object is created, it will automatically call the constructor, that is, use the new key...