PHP5 this,self and Parent keyword detailed tutorial _php basics

Source: Internet
Author: User
First of all, we have to understand the above three keywords: this,self,parent, literally better understanding, refers to this, his father, oh, more fun, we first set up a few concepts, these three key words are used in what place? Let us explain briefly This is a pointer to the current object (let's look at the pointer in c), self is a pointer to the current class, and parent is a pointer to the parent class. We often use pointers here to describe, because there is no better language to express, hehe, Chinese did not learn. -_-#
So it's not very well understood, so let's talk about it in terms of actual examples.
(1) This
Copy Code code as follows:

<?php
Class UserName
{
Defining attributes
Private $name;
Defining constructors
function __construct ($name)
{
$this->name = $name; The this pointer is already used here
}
destructor
function __destruct () {}
Print User name member functions
function Printname ()
{
Print ($this->name); This pointer is also used
}
}
Instantiating an Object
$nameObject = new UserName ("Heiyeluren");
Performing printing
$nameObject->printname (); Output: Heiyeluren
Second instantiation of Object
$nameObject = new UserName ("PHP");
Performing printing
$nameObject->printname (); Output: PHP
?>

Let's see, the class above uses the this pointer, respectively, in rows and lines, so who is this pointing to? In fact, this is when the instantiation to determine who to point to, such as the first instantiation of the object (line), then this is to point to the $nameobject object, then the execution of the line print ($this-><name) into print ($nameObject->name), then of course the "Heiyeluren" is exported. In the second instance, print ($this->name) becomes print ($nameObject->name), and "PHP" is exported. So, this is a pointer to the current object instance and does not point to any other object or class.
(2) Self
First, let's be clear that self is pointing to the class itself, which is that self does not point to any object that has already been instantiated, and general self uses it to point to static variables in the class.
Copy Code code as follows:

<?php
Class Counter
{
Define attributes, including a static variable
private static $firstCount =;
Private $lastCount;
Constructors
function __construct ()
{
$this->lastcount = ++selft:: $firstCount; Use self to invoke a static variable that must be invoked using the Self::(field operator symbol)
}
Print the maximum number of times
function Printlastcount ()
{
Print ($this->lastcount);
}
}
Instantiating an Object
$countObject = new Counter ();
$countObject->printlastcount (); Output
?>

Here we just need to pay attention to two places, line and line. We defined a static variable $firstcount in the second row, and the initial value is, then when the line is called this worthwhile, using self to invoke, and the middle uses "::" To connect, is what we call the domain operator, then we call the class itself defined static variable $ Frestcount, our static variable has nothing to do with the instance of the following object, it's just about the class, then I call the class itself, so we can't use this to refer to it, we can use self to refer to it, because self is pointing to the class itself, regardless of any object instance. In other words, if the static members of our class, we must also use self to invoke.
(3) Parent
We know that parent is a pointer to a parent class, and generally we use parent to invoke the constructor of the parent class.
Copy Code code as follows:

<?php
Base class
Class Animal
{
Properties of the base class
Public $name; Name
Constructor for base class
Public function __construct ($name)
{
$this->name = $name;
}
}
Derived classes
Class Person extends Animal//person class inherits the Animal class
{
Public $personSex; Gender
Public $personAge; Age
Constructors for inheriting classes
function __construct ($personSex, $personAge)
{
Parent::__construct ("Heiyeluren"); The constructor of the parent class was called using parent
$this->personsex = $personSex;
$this->personage = $personAge;
}
function Printperson ()
{
Print ($this->name. ' is '. $this->personsex. ", this year". $this->personage);
}
}
Instantiating a Person object
$personObject = new Person ("male", "");
Performing printing
$personObject->printperson (); Output: Heiyeluren is male,this year
?>

We note a few details: the member properties are public, especially the parent class, for the inheriting class to access through this. We pay attention to the key, line: Parent::__construct ("Heiyeluren"), when we use parent to invoke the constructor of the parent class to initialize the parent class because the members of the parent class are public. So we can invoke it directly in the inheritance class using this.
Summarize:
This is a pointer to an object instance, self is a reference to the class itself, and parent is a reference to the parent class.
Basically I know so much, there must be some understanding of the wrong place, please point out the master!

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.