The difference between self and static in PHP object-oriented programming

Time:2019-10-21

This paper introduces the difference between self and static in PHP object-oriented programming. To share with you for your reference, as follows:

1. Suppose we have a car class, which has two methods:model()andgetModel()

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

After instantiation, the invocation method is:

$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 car class:

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

We know that the subclass will inherit the method of the parent class, and we override the getmodel () method in the subclass Mercedes.
In this case, instantiating the Mercedes class and calling the model() method will output the string “I am car” or “I am Mercedes”?

You may think the result is: I am Mercedes.

But the actual output is:

I am car

why?

3. The working principle of the keyword self is as follows:It will call the method of the current class

becausemodel()Method is only defined in car class, so for self, the current class is car class.self::getModel()Is to call the getmodel method in the car class.

It seems that this is not what we want, how to solve it?

4. Solution 1: overwrite in Mercedes classmodel()Method

class Mercedes extends Car
{
 public function model(){
  //Here we use the keyword self
  self::getModel();
 }
 protected function getModel()
 {
  echo "I am mercedes";
 }
}
//Call the model () method after instantiation
$mercedes = new Mercedes();
$Mercedes - > model(); // output: I am Mercedes

But this is definitely not a good way. The methods have been rewritten, and they still inherit a Mao.

5. Solution 2: change self keyword to static

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

We just replaced the self in the car class with static, and didn’t change the Mercedes class.

At this time, we call:

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

6, summary

A new feature has been added to php5.3, calledDelay static binding。 Can help us achievepolymorphic

To put it simply,Delaying 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 information about PHP, please refer to the following topics: PHP object-oriented programming introduction, PHP array operation skills, PHP basic syntax introduction, PHP operation and operator usage summary, PHP string usage summary, PHP + MySQL database operation introduction and PHP common database operation Skills summary

I hope that this article will be helpful for PHP programming.