PHP Object-oriented strategy (IV.) construction method and _php foundation of destructor method

Source: Internet
Author: User
8. Construction method and the method of destructor
Most classes have a special method called constructors. When an object is created, it automatically invokes the constructor
Number, which means that the constructor method is invoked automatically when instantiating an object using the New keyword.
The declaration of a constructor is the same as the declaration of another operation, except that its name must be __construct (). This is PHP5.
Changes in previous versions, constructors must have the same name as the class name, which can still be used in PHP5, but
Now, with few people using it, the advantage is that you can make the constructor independent of the class name, and not when the class name changes
The corresponding constructor name needs to be changed. For backward compatibility, if there is no method named __construct () in a class,
PHP will search for a method of writing in PHP4 with the same name as the class name.
Format: function __construct ([parameters]) {...}
Only one construction method can be declared in a class, but it is invoked once every time the object is created
method, which cannot be invoked voluntarily, so it is usually used to perform some useful initialization tasks. such as the genus
Sex assigns an initial value when the object is created.
Code fragment
Copy Code code as follows:

?
Create a human
Class person{
The following are the member properties of the person
var $name; Person's name
var $sex; The gender of the person
var $age; The age of the person
Define a constructor parameter for name $name, gender $sex and age $age
function __construct ($name, $sex, $age) {
$name to member property $this->name by constructing a method
$this->name= $name;
$sex to member property $this->sex by constructing a method
$this->sex= $sex;
$age to member property $this->age by constructing a method
$this->age= $age;
}
This man's way of speaking
function say () {
echo "My name is:". $this->name. "Sex:". $this->sex. "My Age is:". $this->age. " <br> ";
}
}
Create 3 Objects $p1, P2, $p 3 by constructing a method, passing in three different arguments to name, gender, and age, respectively.
$p 1=new person ("John", "Male", 20);
$p 2=new person ("Dick", "female", 30);
$p 3=new person ("Harry", "male", 40);
The following accesses the speech method in the $p1 object
$p 1->say ();
The following accesses the speech method in the $p2 object
$p 2->say ();
The following accesses the speech method in the $p3 object
$p 3->say ();
?>

The output results are:
My name is: John Sex: Male My age is: 20
My name is: Dick Sex: Female my age is: 30
My name is: Harry Sex: Male My age is: 40
As shown in figure:
' This.width= '; if (this.height> ') ' this.height= '; border=0>
destructor:
The opposite of the constructor is the destructor. Destructors are new additions to PHP5, and there is no analysis in PHP4
Construct function. Destructors allow you to perform some operations or perform some functions before destroying a class, such as closing a file,
Releasing the result set, and so on, the destructor executes when all references to an object are deleted or when an object is explicitly destroyed.
That is, the object calls the destructor before it is destroyed in memory. Similar to the name of the constructor, the name of the destructor function for a class
Said to be __destruct (). Destructors cannot have any arguments.
Format: function __destruct () {...}
Code fragment
Copy Code code as follows:

?
Create a human
Class person{
The following are the member properties of the person
var $name; Person's name
var $sex; The gender of the person
var $age; The age of the person
Define a constructor parameter for name $name, gender $sex and age $age
function __construct ($name, $sex, $age) {
$name to member property $this->name by constructing a method
$this->name= $name;
$sex to member property $this->sex by constructing a method
$this->sex= $sex;
$age to member property $this->age by constructing a method
$this->age= $age;
}
This man's way of speaking
function say () {
echo "My name is:". $this->name. "Sex:". $this->sex. "My Age is:". $this->age. " <br> ";
}
This is a destructor that is called before the object is destroyed
function __destruct () {
echo "Goodbye". $this->name. " <br> ";
}
Create 3 Objects $p1, P2, $p 3 by constructing a method, passing in three different arguments to name, gender, and age, respectively.
$p 1=new person ("John", "Male", 20);
$p 2=new person ("Dick", "female", 30);
$p 3=new person ("Harry", "male", 40);
The following accesses the speech method in the $p1 object
$p 1->say ();
The following accesses the speech method in the $p2 object
$p 2->say ();
The following accesses the speech method in the $p3 object
$p 3->say ();
?>

The output results are:
My name is: John Sex: Male My age is: 20
My name is: Dick Sex: Female my age is: 30
My name is: Harry Sex: Male My age is: 40
Bye, John.
Bye, Dick.
Bye, Harry.

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.