Php object-oriented tutorial 4

Source: Internet
Author: User
Php object-oriented tutorial 4 7. Special reference of "$ this"
Now we know how to access the members in the object. they are accessed through "object-> Member". This is outside the object.
If you want to allow methods in the object to access the attributes of the object within the object, or
Is the method in the object to call other methods of this object. what should we do? Because all the members in the object must use
Like to call, including calling between internal members of an object, so in PHP, we provide a reference to this object $ this,
Each object has an object reference $ this to represent this object. this is called by the members of the object.
In the instance above, we instantiate three instance objects $ P1, $ P2, and $ P3.
$ This indicates $ p1, $ p2, and $ p3 respectively.
We can see that $ this represents the reference of this object inside the object.
Members use the same method as external call objects.

$ This-> attribute $ this-> name; $ this-> age; $ this-> sex; $ this-> method $ this-> say (); $ this-> run ();

Modify the instance above to allow everyone to name themselves, gender and age:

 Name. "Gender:". $ this-> sex. "My age is:". $ this-> age ."
";}Function run () // The way this Person can walk {echo" this Person is walking ";}}$ p1 = new Person (); // Create an instance object $ p1 $ p2 = new Person (); // Create an instance object $ p2 $ p3 = new Person (); // Create an instance object $ p3 // the following three rows assign $ p1 object attributes $ p1-> name = "Zhangsan"; $ p1-> sex = "male "; $ p1-> age = 20; // The method for accessing the $ p1 object $ p1-> say (); // the following three rows assign $ p2 object attributes $ p2-> name = "Li Si"; $ p2-> sex = "female"; $ p2-> age = 30; // the following method of accessing the $ p2 object $ p2-> say (); // the following three rows assign $ p3 object attributes $ p3-> name = "Wang Wu "; $ p3-> sex = "male"; $ p3-> age = 40; // The following method of accessing 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: 40

Analyze this method:

Function say () // The way this person can talk {echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";}

The say () method is available for $ p1, $ p2, and $ p3 objects. $ this represents the three objects and calls the corresponding
And print the attribute value. this is the way to access the object attribute within the object. if you call
You can also use the run () method to complete the call by using the $ this-> run () method in the say () method.
8. constructor and constructor
Most classes have a special method called constructor. When an object is created, it automatically calls the constructor.
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 a change in PHP5,
In previous versions, the name of the constructor must be the same as the class name, which can still be used in PHP5, but it is rarely
The advantage of doing so is that the constructor can be independent of the class name. when the class name changes, the corresponding constructor is not required.
Name. For backward compatibility, if a class does not contain the _ construct () method, PHP searches for a write in php4
Method, which is the same as the class name.
Format:

Function _ construct ([parameter]) {......}

Only one constructor can be declared in a class, but the constructor is called only once each time an object is created,
This method cannot be called proactively, so it is usually used to execute some useful initialization tasks. For example
Assign the initial value.

 Initial value $ this-> name = $ name; // The $ sex passed in through the constructor gives the Member attributes $ this-> sex the initial value $ this-> sex = $ sex; // assign the $ age passed in by the constructor to the member attribute $ this-> age with the initial value $ this-> age = $ age ;} // function say () {echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";}}// Create three objects $ p1, p2, and $ p3 through the constructor, 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 ); // access the method of speaking in the $ p1 object below $ p1-> say (); // access the method of speaking in the $ p2 object below $ 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: 40


Destructor:
The constructor is opposite to the constructor. The Destructor is newly added to PHP5 and does not contain the Destructor in PHP4.
The Destructor allows you to perform operations or complete some functions before destroying a class, such as closing a file and releasing a result set,
The Destructor is executed when all references to an object are deleted or when the object is explicitly destroyed, that is, the object is in the memory.
Call the destructor before destruction. Similar to the constructor name, the destructor name of a class must be _ destruct (). Structure Analysis
A function cannot contain any parameters.
Format:

function __destruct ( ) { ... ... }
 Initial value $ this-> name = $ name; // The $ sex passed in through the constructor gives the Member attributes $ this-> sex the initial value $ this-> sex = $ sex; // assign the $ age passed in by the constructor to the member attribute $ this-> age with the initial value $ this-> age = $ age ;} // function say () {echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";}// This is a destructor. before the object is destroyed, call function _ destruct () {echo" goodbye ". $ this-> name ."
";} // Create three objects $ p1, p2, and $ p3 through the constructor, 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 ); // access the method of speaking in the $ p1 object below $ p1-> say (); // access the method of speaking in the $ p2 object below $ 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: my male's age is: 40 goodbye Zhang San goodbye Li Si goodbye Wang Wu

The above is the content of php object-oriented tutorial 4. For more information, see PHP Chinese network (www.php1.cn )!

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.