Difference Analysis of self and static in php Object Oriented Programming

  • 2021-12-11 07:07:02
  • OfStack

This paper describes the difference between self and static in php object-oriented programming. Share it for your reference, as follows:

1. Suppose we have an Car class that has two methods: model() And getModel() .


class Car{
 public function model(){
  // Here we use keywords self
  self::getModel();
 }
 protected function getModel(){
  echo 'I am car';
 }
}

Invoke the method after instantiation:


$car = new Car();
$car->model(); //  Output: I am car

The keyword self causes us to call the getModel () method of the Car class and output the text "I am car".

2. Add a new class as a subclass of the Car class:


class Mercedes extends Car
{
 protected function getModel()
 {
  echo "I am mercedes";
 }
}
//  After instantiation   Call model() Method 
$mercedes = new Mercedes();
$mercedes->model();

We know that the subclass inherits the parent's method, and we override the getModel () method in the subclass Mercedes.
At this time, the instantiated Mercedes class calls the model () method, and will it output the string "I am car" or "I am mercedes"?

You might think the result is: I am mercedes.

But the actual output is:

I am car

why?

3. The keyword self works by calling the methods of the current class.

Because model() Methods are defined only in the Car class, so for self the current class is the Car class. self::getModel() Is to call the getModel method in the Car class.

This doesn't seem to be what we want. How to solve it?

4. Solution 1: Override in the Mercedes class model() Method


class Mercedes extends Car
{
 public function model(){
  // Here we use keywords self
  self::getModel();
 }
 protected function getModel()
 {
  echo "I am mercedes";
 }
}
//  After instantiation   Call model() Method 
$mercedes = new Mercedes();
$mercedes->model(); //  Output: I am mercedes

But this is definitely not a good way. All the methods have been rewritten, and they still inherit a hair.

5. Solution 2: Replace the self keyword with static


class Car{
 public function model(){
  // Here we use keywords static
  static::getModel();
 }
 protected function getModel(){
  echo 'I am car';
 }
}

We only replaced self in the Car class with static, and did not modify the Mercedes class.

At this point we call:


$mercedes = new Mercedes();
$mercedes->model(); //  Output: I am mercedes

6. Summary

A new feature called delayed static binding has been added to php 5.3. It can help us achieve polymorphism.

Simply put, deferred static binding means that when we call an inherited method using the static keyword, it will bind the calling class at run time.

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this paper is helpful to everyone's PHP programming.


Related articles: