PHP Object-Oriented learning Parent:: keyword

Source: Internet
Author: User
Tags class manager
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

<!--declaration of an employee class, the manager class inherits from the 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.//You must first call the Getsal () method of the parent class. Public Function Getsal () {  Parent:: Getsal ();//The method of the parent class is called here. $this->sal = $this->sal + 1500; return $this->sal; }} $emp = new employee (); Echo "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.

<?phpclass 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.

<?phpclass 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 () {//This returns the PRI of the parent class The Vate property. 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.

<?phpclass 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.

<?phpclass employee{Private $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

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.

<?phpclass 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.

<?phpclass 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 ();

Summarize

The above is the entire content of this article, I hope that the content of this article on everyone's study or work can bring certain help, if there is doubt you can message exchange.

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.