PHP Object-Oriented tutorial 4

Source: Internet
Author: User
7. Use of special reference "$this"
Now we know how to access the members of the object, accessed through the object-to-member method, which is outside the object
section to access the form of the member in the object, then if I want to inside the object, let the object's methods access the object's properties, or
Is the method in the object to invoke other methods of this object what do we do then? Because all the members of the object have to use the
Like to invoke, including calls between the internal members of the object, so in PHP I provide a reference to this object $this,
Each object has a reference to the object $this to represent the object, complete the invocation of the internal members of the object, the intent of this is
"This" means, in the above example, we instantiate three instance objects $p1, $P 2, $P 3, the three objects inside each
The self-existing $this represents the object $p1, $p 2, $p 3 respectively.
As we can see, the $this is the reference within the object that represents this object, inside the object and invoking the object's
Members are used in the same way as members of objects that are called outside the object.

$this-Property $this->name; $this->age; $this->sex; $this, Method $this->say (); $this->run ();

Modify the example above so that everyone speaks their name, gender and age:

<?phpclass person{//Below is a member of the person attribute var $name;//person's name Var $sex;//gender var $age;//person's age//The following is the person's member method function say ()//This person can speak the way {echo "My name is:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age. " <br> ";} function run ()//This person can walk the way {echo "This person is walking";}} $p 1=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 lines are assigned to the $p1 object property $p1->name= "Zhang San", $p 1->sex= "male", $p 1->age=20;//below to access $p1 object of speech method $p1->say ();/ The following three lines are assigned to the $p2 object property $p2->name= "John Doe", $p 2->sex= "female", and $p 2->age=30;//to access $p2 object's Speech method $p2->say ();//The following three lines are for $ P3 object property Assignment $p3->name= "Harry", $p 3->sex= "male", $p 3->age=40;//below to access $p3 object of speech method $p3->say (); >

The output is:

My name is: Zhang San Sex: Male my age is: 20 My name is: John Doe sex: Female my age is: 30 My name is: Harry Sex: Male My age is: 40

Analyze This method:

function say ()//This person can speak the way {echo "My name is:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age. " <br> ";}

In the $P1, $p 2, and $p3 all three objects have say () This method, $this represent the three objects respectively, call the corresponding
property to print out the value of the property, which is how the object's properties are accessed inside the object, if the Say () method
It is also possible to use the run () method, which is used in the Say () method to complete the invocation using $this->run ().
8. Construction method and destructor method
Most classes have a special method called constructors. When an object is created, it automatically calls the constructor and also
is to automatically invoke the constructor method when instantiating an object using the New keyword.
The declaration of the constructor is the same as the declaration of the other operation, except that its name must be __construct (). This is a change in the PHP5,
In previous versions, the constructor name must be the same as the class name, which can still be used in PHP5, but is now subject to very few
The benefit of this is that you can make the constructor independent of the class name and do not need to change the constructor when the class name changes
The name. To be backwards compatible, if there is no method named __construct () in a class, PHP will search for a write in a php4
method, constructed with the same name as the class name.
Format:

function __construct ([parameter]) {...}

Only one construction method can be declared in a class, but a constructor is called every time the object is created.
This method cannot be invoked actively, so it is often used to perform some useful initialization tasks. For example, an attribute is created in the object's
Time to assign an initial value.

<?//Create a Human class person{//below is the person's member attribute Var $name;//person's name Var $sex;//Human gender var $age;//person's age//define a constructor method parameter for name $name, gender $sex and age $ Agefunction __construct ($name, $sex, $age) {//the $name passed in through the constructor method gives the member property $this->name the value $this->name= $name;// The $sex that is passed in by the constructor method assigns the value $this->sex= $sex to the member property $this->sex, or the $age that is passed in by the constructor method assigns the value $this->age to the member property $this->age=$ Age;} This person's way of speaking function say () {echo "My name is:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age. " <br> ";}} Create 3 objects by constructing the method $p1, P2, $p 3, passing in three different arguments for name, gender, and age $p1=new person ("Zhang San", "male"), $p 2=new person ("John Doe", "female"), $p 3=new Person ("Harry", "male", 40);//The following access to the $p1 object in the speech method $p1->say ();//The following access to the $p2 object in the speech method $p2->say ();//The following access to the $p3 object in the speech method $p3->say ();? >

The output is:

My name is: Zhang San Sex: Male my age is: 20 My name is: John Doe sex: Female my age is: 30 My name is: Harry Sex: Male My age is: 40


Destructors:
A destructor is the opposite of a constructor function. Destructors are new additions to PHP5, and there are no destructors in PHP4.
Destructors allow some operations to be performed before destroying a class or to perform functions such as closing a file, releasing a result set, etc.
Destructors are removed from all references to an object or executed when an object is explicitly destroyed, that is, the object is in memory
The destructor is called before being destroyed. Like the name of a constructor, a class's destructor name must be __destruct (). Destruction
The function cannot have any parameters.
Format:

function __destruct () {...}
<?//Create a Human class person{//below is the person's member attribute Var $name;//person's name Var $sex;//Human gender var $age;//person's age//define a constructor method parameter for name $name, gender $sex and age $ Agefunction __construct ($name, $sex, $age) {//the $name passed in through the constructor method gives the member property $this->name the value $this->name= $name;// The $sex that is passed in by the constructor method assigns the value $this->sex= $sex to the member property $this->sex, or the $age that is passed in by the constructor method assigns the value $this->age to the member property $this->age=$ Age;} This person's way of speaking function say () {echo "My name is:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age. " <br> ";} This is a destructor that calls function __destruct () {echo "Goodbye" before the object is destroyed. $this->name. " <br> ";} Create 3 objects by constructing the method $p1, P2, $p 3, passing in three different arguments for name, gender, and age $p1=new person ("Zhang San", "male"), $p 2=new person ("John Doe", "female"), $p 3=new Person ("Harry", "male", 40);//The following access to the $p1 object in the speech method $p1->say ();//The following access to the $p2 object in the speech method $p2->say ();//The following access to the $p3 object in the speech method $p3->say ();? >

The output is:

My name is: Zhang San Sex: Male my age is: 20 My name is: John Doe sex: Female my age is: 30 My name is: Harry Sex: Male My age is: 40 goodbye Zhang San goodbye John Doe Goodbye Harry

The above is the PHP object-oriented tutorial 4 content, more relevant content please pay attention to topic.alibabacloud.com (www.php.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.