PHP Object-oriented Parent:: How is the keyword used?

Source: Internet
Author: User
You cannot define a function in PHP that has the same name, or a method that cannot be defined in the same class, so there is no method overload.

Objective

Recently in the Thinkphp development project, the use of the parent:: keyword, in fact, the parent:: The keyword is commonly used in PHP a function, this is not only in the thinkphp project development, even a small enterprise site, There will also be many same function functions that we can encapsulate into a parent class, and then multiple subclasses directly inherit from the public part of the parent class.

In PHP5, use the parent:: To refer to the method of the parental class.

Parent:: Can be used to invoke a member method defined in the parent class.

Parent:: Is traced not only to the immediate parent class.

Through Parent:: Call the Parental method

<!--declare an employee class, the manager class inherits from employee Class--<? Class employee{  protected $sal =3000;   Public Function Getsal () {  $this->sal = $this->sal +;  return $this->sal;  } }  Class Manager extends Employee {  //If you want the manager to make more than 1500 yuan on the basis of the employee's salary.  The Getsal () method of the parent class must be called first.  Public Function Getsal () {   parent::getsal ();//The method of the parent class is called here.  $this->sal = $this->sal +;  return $this->sal;  } } $emp = new employee (); echo "The average employee's salary is". $emp->getsal (); echo "<br>"; xiariboke.com $manager = new manager (); echo "Manager's salary is:". $manager->getsal ();?>

Program Run Result:

The average employee's salary is 4200.

The manager's salary is: 5700

Private property of the parent class

This thing is very uncomfortable to explain.

The private property cannot be inherited if the parent class has a private property. Then the parent class's method is only a private property service for the parent class.

The following example looks strange, redefining an attribute $sal in a subclass, and the system returns the properties of the parent class.

<?php class employee{  private $sal =3000;  protected $sal =3000;  Public Function Getsal () {   return $this->sal;  }  }  Class Manager extends Employee {  protected $sal =5000;   Public Function Getparentsal () {  //The private property of the parent class is returned here.  return Parent::getsal ();  } } $manager = new manager (); echo "PHP". Phpversion (). " <br> "; echo $manager->getsal (); echo "<br>"; echo "Parent ' s \ $sal". $manager->getparentsal (); Xiariboke.com?>

Program Run Result:

PHP 5.2.9

3000

Parent ' s $sal 3000

If the property in the parent class is overridden by the quilt class. The result is this. Note that the property definition for line 5th becomes protected.

<?php class employee{  //private $sal =3000;  protected $sal =3000;  Public Function Getsal () {   return $this->sal;  }  }  Class Manager extends Employee {  protected $sal =5000;  Xiariboke.com Public  function Getparentsal () {  //The private property of the parent class is returned here.  return Parent::getsal ();  } } $manager = new manager (); echo "PHP". Phpversion (). " <br> "; echo $manager->getsal (); echo "<br>"; echo "Parent ' s \ $sal". $manager->getparentsal ();  ? >

Program Run Result:

PHP 5.2.9

5000

Parent ' s $sal 5000

Methods overridden in subclasses are valid for the current private.

<?php class employee{  private $sal =3000;  Public Function Getsal () {   return $this->sal;  }}  Class Manager extends Employee {  private $sal =5000;  Overridden method public  function Getsal () {   return $this->sal;  }   Public Function Getparentsal () {  //The private property of the parent class is returned here.  return Parent::getsal ();  } } $manager = new manager (); echo "PHP". Phpversion (). " <br> "; echo $manager->getsal (); echo "<br>"; echo "Parent ' s \ $sal". $manager->getparentsal ();  ? >

Program Run Result:

PHP 5.2.9

5000

Parent ' s $sal 3000

Open the Zend debug state to see what is happening in memory. Note the bottom, there are two $sal. are 3000 and 5000, respectively.

<?php class employee{  private $sal =3000;  Public Function Getsal () {   return $this->sal;  }  } class Manager extends employee {  protected $sal = ;  Public Function Getparentsal () {  return $this->sal;  }} $manager = new manager (); echo "PHP". Phpversion (). " <br> "; echo $manager->getsal ();  ? >

Program Run Result:

PHP 5.2.9

3000

Changes the parent class's property $sal to protected, and the subclass overrides the parent class's properties. There is only one $sal in memory.

<?php class employee{  protected $sal =3000;  Public Function Getsal () {   return $this->sal;  }  } class Manager extends employee {  protected $sal =5000;  Public Function Getparentsal () {  return $this->sal;  }} $manager = new manager (); echo "PHP". Phpversion (). " <br> "; echo $manager->getsal ();  ? >

Program Run Result:

PHP 5.2.9

5000

If you've learned Java, you'll find it hard to understand.

In Java, when a subclass is created, the properties and methods of the parent class are created in memory, and even constructors are called.

PHP5 This is not the case, PHP5 calling the parent class is using parent: instead of parent->, which is enough to show that PHP5 does not want the parent class to be created in memory. PHP5 to make inheritance more simple than Java.

Adapt to the good.

Such a call would cause PHP5.1.1 to overflow. The new version does not know if there is a problem.

<?php class employee{  private $sal =3000;  Public Function Getsal () {   return Parent:: $this->sal;  }  }  Class Manager extends Employee {  protected $sal =5000;  Public Function Getsal () {   return Parent:: $this->getsal ();  }} $manager = new manager (); echo "PHP". Phpversion ()." <br> "; echo $manager->getsal ();  ? >

It would be nice to change the 12th line. Note the comparison.

return parent:: getSal();

Such code causes a recursive operation, the subclass calls the method of the parent class, and the parent class calls the subclass method.

return parent::$this->getSal();

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.