Differences and usage of this, self and parent in PHP object-oriented

Source: Internet
Author: User

This is a pointer to the object instance. self is a reference to the class itself, and parent is a reference to the parent class.

This:

<? PHP

Class Username
{
// Define attributes
Private $ name;
 
// Define the constructor
Function _ construct ($ name)
{
$ This-> name = $ name; // This pointer is already used here
}
 
// Destructor
Function _ destruct (){}

// Print the username member function
Function printname ()
{
Print ($ this-> name); // This pointer is used again
}
}

// Instantiate the object
$ Nameobject = new username ("heiyeluren ");

// Execute print
$ Nameobject-> printname (); // output: heiyeluren
 
// The second Object Instantiation
$ Nameobject = new username ("php ");
 
// Execute print
$ Nameobject-> printname (); // output: PHP
?>

Note: This cannot reference static variables. This is to point to the current object.

 

SELF:

<? PHP

Class counter
{
// Define attributes, including a static variable
Private Static $ firstcount =;
Private $ lastcount;

// Constructor
Function _ construct ()
{
$ This-> lastcount = ++ selft: $ firstcount; // use self to call static variables. To use self to call static variables, you must use: (domain operator number)
}

// Print the maximum value
Function printlastcount ()
{
Print ($ this-> lastcount );
}
}

// Instantiate the object
$ Countobject = new counter ();

$ Countobject-> printlastcount (); // output

 ?> 

Note: Self points to the class itself, that is, self does not point to any instantiated object. Generally, self points to static variables and static methods in the class.

Whenever self uses the domain operator: static variables used by the called static variables or static methods must be initialized; otherwise, the variable is not initialized.

Note that the call to static variables must contain $

 

Parent:

 

<? PHP

// Base class
Class animal
{
// Attributes of the base class
Public $ name; // name

// Constructor of the base class
Public Function _ construct ($ name)
{
$ This-> name = $ name;
}
}

// Derived class
Class person extends animal // person class inherits animal class
{
Public $ personsex; // gender
Public $ personage; // age

// Constructor of the inherited class
Function _ construct ($ personsex, $ personage)
{
Parent: :__ construct ("heiyeluren"); // uses parent to call the constructor of the parent class.
$ This-> personsex = $ personsex;
$ This-> personage = $ personage;
}

Function printperson ()
{
Print ($ this-> name. "Is". $ this-> personsex. ", this year". $ this-> personage );
}
}

// Instantiate the person object
$ Personobject = new person ("male ","");

// Execute print
$ Personobject-> printperson (); // output: heiyeluren is male, this year

 ?> 

 

We know that parent is a pointer to the parent class. Generally, we use parent to call the constructor and method of the parent class. It also uses the domain operator: Call, and cannot call attributes.

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.