This,self,parent Differences and PHP double colons: Usage

Source: Internet
Author: User
Tags getcolor
This,self,parent Differences and PHP double colons:: Usage

PHP5 is a language with most of the object-oriented language features, more than PHP4 has a lot of object-oriented features, but some concepts are also more around people, so today take out to say, say bad, please master forgive me. (Read this article to learn about PHP5 's object-oriented knowledge)
First of all, we understand three keywords: this,self,parent, from the literal better understanding, refers to this, their own, father, hehe, more fun, we first set up a few concepts, the three key words are used in what place? Let us briefly explain that this is a pointer to the current object ( Let's take a look at the pointer in c), self is a pointer to the current class, and parent is a pointer to the parent class.

That's not quite understood, so let's talk about it in terms of practical examples.
(1) This
Class UserName
{
??? Define Properties???
???? Private $name;
???? Defining constructors
??? function __construct ($name)
???? {
????????? $this->name = $name; The this pointer is already used here
???? }
???? Destructors
???? function __destruct () {}
???? Print User name member function
???? function Printname ()
???? {
????????? Print ($this->name); The this pointer is also used
???? }
}
Instantiating an Object
$nameObject = new UserName ("Heiyeluren");
Perform printing
$nameObject->printname (); Output: Heiyeluren
Second instantiation of the object
$nameObject 2 = new UserName ("PHP5");
Perform printing
$nameObject 2->printname (); Output: PHP5
?>
Let's see, the class above uses the this pointer in 11 rows and 20 rows, so who is this at that point? In fact this is at the time of instantiation to determine who to point to, such as when the first instantiation of the object (25 rows), then this is pointing to the $nameobject object, then the execution of 18 lines of printing when the print ($this- name), then of course the "Heiyeluren" is output. In the second instance, print ($this->name) becomes print ($nameObject 2->name), so it outputs "PHP5". So, this is a pointer to the current object instance and does not point to any other object or class.
?
?
(2) Self
First we have to make it clear that self is pointing to the class itself, that is, that it does not point to any object that has already been instantiated, and that the normal.
!--? php ???? class Counter
???? {
????????//define properties, including a static variable
???????? private static $firstCount = 0;
???????? Private $lastCount;
???????//constructor
???????? function __construct ()
???????? {
????????????? $this->lastcount = ++selft:: $firstCount;//using Self to invoke a static variable, using the self call must use::(domain operator symbol)
????????}
????????//print maximum value
???????? function printlastcount ()
???????? {
?????????????? print ($this->lastcount);
????????}
???}
//Instantiate object
$countObject = new Counter ();
$countObject->printlastcount ();//Output 1
?>

We just need to pay attention to two places, lines 6th and 12th. We define a static variable $firstcount in the second row, and the initial value is 0, then it is worth to call this in 12 rows, using self to call, and the middle using "::" To connect, is what we call the domain operator, So at this point we call the class itself to define the static variable $ frestcount, our static variable is not related to the instance of the following object, it is only associated with the class, then I invoke the class itself, then we can not use this to refer to, the use of self to reference, Because self is pointing to the class itself, it is independent 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 the parent class, and generally we use the parent to invoke the constructor of the parental class.
Base class
Class Animal
{
??? Properties of the base class
???? Public $name; Name
??? Constructors for base classes
??? Public function __construct ($name)
???? {
???????? $this->name = $name;
????? }
}
Derived classes
Class Person extends Animal//person classes inherit the Animal class
{
??? Public $personSex; Gender
??? Public $personAge; Age
???? Constructors for inheriting classes
???? function __construct ($personSex, $personAge)
??? {
?????????? Parent::__construct ("Heiyeluren"); The constructor for the parent class was called with 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", "21");
Perform printing
$personObject->printperson (); Output: Heiyeluren is male,this year 21
?>

We pay attention to a few details: the member properties are public, especially the parent class, for the inheriting class to access through this. We note the key point, line 25th: Parent:: __construct ("Heiyeluren"), when we use the parent to invoke the constructor of the parents to initialize the parent class, because the members of the parent class are public. We are then able to invoke this directly in the inheriting class using this.


In general this is the current object instance, statically static variable, const constant, constructor, regardless of the instance of the object, it is only related to the class, so use:


Program List: Use variables outside the class definition to access the
??? !--? php ??? class Fruit {
??????? Const Const_value = ' Fruit Color ';
???}
????
??? $classname = ' Fruit ';
??? echo $classname:: Const_value;//As of PHP 5.3.0
????
??? echo fruit::const_value;
????;
Program List: Used outside the class definition::
??? !--? php ??? class Fruit {
??????? Const Const_value = ' Fruit Color ';
???}
????
??? Class Apple extends Fruit
??? {
??????? public static $color = ' Red ';
????
??????? public static function Doublecolon () {
??????????? echo parent::const_value. "\ n";
??????????? echo Self:: $color. "\ n";
???????}
???}
????
??? Apple::d Oublecolon ();
????;

Program Run Result:
1??? Fruit Color Red
Program List: Call the parent method
??? !--? php ??? class Fruit
??? {
??????? protected function Showcolor () {
??????????? echo "Fruit::showcolor () \ n";
??????}
???}
????
??? Class Apple extends Fruit
??? {
???????//Override parent ' s definition
??????? Public Function Showcolor ()
??????? {
??????????/Still call the parent function
??????????? Parent::showcolor ();
??????????? echo "Apple:: Showcolor () \ n ";
???????}
???}
????
??? $apple = new Apple ();
?? $apple->showcolor (); not static
????;

Program Run Result:
1??? Fruit::showcolor ()
2??? Apple::showcolor ()
Program List: Use scope qualifier
??? !--? PHP ??????? class Apple
??????? {
??????????? Public Function Showcolor ()
??????????? {
??????????????? return $this->color;
???????????}
???????}
????
??????? class Banana
??????? {
??????????? public $color;
????
??????????? Public Function __construct ()
??????????? {
??????????????? $this->color = "Banana is yellow";
???????????}
????
??????????? Public Function GetColor ()
??????????? {
??????????????? return apple::showcolor ();///Use scope qualifier
???????????}
???????}
????
??????? $banana = new Banana;
??????? Echo $banana->getcolor ();
????;

Program Run Result:
1??? Banana is yellow
Program List: Methods for calling base classes
??? ????
??? Class Fruit
??? {
??????? static function Color ()
?????? {
??????????? return "COLOR";
??????? }
????
??????? static function Showcolor ()
??????? {
?????????? echo "Show". Self::color ();
??????? }
??? }
????
??? Class Apple extends Fruit
??? {
??????? static function Color ()
??????? {
??????????? return "Red";
??????? }
??? }
????
??? Apple::showcolor ();
??? Output is "Show color"!
????
??? ?>

Program Run Result:
1??? Show Color

?

?

?

  • 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.