Home  >  Article  >  Backend Development  >  What you need to know about the differences between this, self, and parent during PHP programming_PHP Tutorial

What you need to know about the differences between this, self, and parent during PHP programming_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:42:13845browse

{一}One of the differences between this, self, parent in PHP this article
Object-oriented programming (OOP, Object Oriented Programming) has now become a basic skill for programmers. Using OOP ideas for advanced programming of PHP is very meaningful for improving PHP programming capabilities and planning web development architecture.
After being rewritten, PHP5 has made a big leap in its support for OOP and has become a language with most of the features of an object-oriented language. It has many more object-oriented features than PHP4. What I am mainly talking about here is the difference between the three keywords this, self, and parent. Literally understood, they refer to this, myself, and father respectively. Let me give a preliminary explanation first. This is a pointer to the current object (can be regarded as a pointer in C), self is a pointer to the current class, and parent is a pointer to the parent class. We frequently use pointers to describe here because there is no better language to express it. Regarding the concept of pointers, you can refer to the encyclopedia.
Let’s talk about it based on actual examples.

Copy code The code is as follows:

class name //Create a class named name Class
{
private $name; //Define attributes, private
//Define constructor for initialization assignment
function __construct( $name)
{
$this- >name = $name; //This pointer statement has been used here①
}
//Destructor
function __destruct(){}
//Print user name member function
function printname()
{
print( $this->name ); //The this pointer statement ② is used again, or echo output can be used
}
}
$obj1 = new name( "PBPHome" ); //Instantiation object statement ③
//Perform printing
$obj1->printname(); //Output: PBPHome
echo "
" ; //Output: Enter
//Second instantiation of the object
$obj2 = new name( "PHP" );
//Perform printing
$obj2->printname() ; //Output: PHP
?>

Explanation: The above class uses this pointer in statement ① and statement ② respectively, so who does this point to at that time? In fact, this determines who it points to when instantiating it. For example, when the object is instantiated for the first time (statement ③), then this points to the $obj1 object at that time. Then when executing the print of statement ②, print( $this- >name ), then of course "PBPHome" is output. In the second instance, print( $this->name ) becomes print( $obj2->name ), so "PHP" is output. Therefore, this is a pointer to the current object instance and does not point to any other object or class.
{two}. The difference between this, self and parent in PHP Part 2 self
In this article we will explain the usage of self
First of all, we have to make it clear that self points to the class itself, that is, self does not point to anything. For objects that have been instantiated, self is generally used to point to static variables in the class. If we use static members in the class (usually using the keyword static), we must also use self to call. Also note that using self to call static variables must use :: (field operator symbol), see example.
Copy code The code is as follows:

class counter //Define a counter class
{
//Define attributes, including a static variable $firstCount, and assign an initial value of 0. Statement ①
private static $firstCount = 0;
private $lastCount;
//Constructor
function __construct()
{
$this->lastCount = ++self::$firstCount; //Use self to call static variable statement ②
}
//Print lastCount value
function printLastCount()
{
print( $this->lastCount );
}
}
//Instantiated object
$obj = new Counter();
$obj->printLastCount(); //When executed here, the program outputs 1
?>

There are two local statements ① and ② that should be noted here. We defined a static variable $firstCount in statement ①, and then used self to call this value in statement ②. Then what we call at this time is the static variable $frestCount defined by the class itself. Our static variable has nothing to do with the instance of the following object, it is only related to the class. Then if I call the class itself, then we cannot use this to refer to it, because self points to the class itself and has nothing to do with any object instance. Then the this used earlier calls the instantiated object $obj, so don’t get confused.
That’s it for self. It’s easier to understand with examples. The end of the second chapter.
{3} The difference between this, self and parent in PHP Part 3 parent
In this article we will explain the usage of parent.
First of all, we make it clear that parent is a pointer to the parent class. Generally, we use parent to call the constructor of the parent class. The example is as follows:
Copy code The code is as follows:

//Create the base class Animal
class Animal
{
public $name; //Attributes of the base class, name $name
// Constructor of base class, initialization assignment
public function __construct( $name )
{
$this->name = $name;
}
}
//Define derivation Class Person inherits from Animal class
class Person extends Animal
{
public $personSex; //For the derived class, newly defined attributes $personSex gender, $personAge age
public $personAge;
//Constructor of the derived class
function __construct( $personSex, $personAge )
{
parent::__construct( "PBPHome" ); //Use parent to call the constructor statement of the parent class ①
$this->personSex = $personSex;
$this->personAge = $personAge;
}
//Member function of derived class, used for printing, format: name is name ,age is age
function printPerson()
{
print( $this->name. " is " .$this->personSex. ",age is " .$this->personAge );
}
}
//Instantiate the Person object
$personObject = new Person( "male", "21");
//Perform printing
$personObject- >printPerson(); //Output result: PBPHome is male,age is 21
?>

It also contains the usage of this, please analyze it yourself. We pay attention to this detail: the member attributes are all public (public attributes and methods, accessible to code inside and outside the class), especially those of the parent class. This is for inherited classes to access through this. The key point is statement ①: parent::__construct( "heiyeluren" ). At this time, we use parent to call the constructor of the parent class to initialize the parent class. In this way, the objects of the inherited class are assigned the name PBPHome. . We can test it by instantiating another object $personObject1. After printing, the name is still PBPHome.
Summary: this is a pointer to an object instance, which is determined when instantiating; self is a reference to the class itself, generally used to point to static variables in the class; parent is a reference to the parent class, Generally, parent is used to call the constructor of the parent class.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321057.htmlTechArticle{一}One of the differences between this, self, parent in PHP this article Object Oriented Programming (OOP, Object Oriented Programming) has now become a basic skill for programmers. Using OOP ideas for PHP...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn