Home >Backend Development >PHP Tutorial >The difference between self and static in php object-oriented programming matlab object-oriented programming c# object-oriented programming ideas object-oriented programming language

The difference between self and static in php object-oriented programming matlab object-oriented programming c# object-oriented programming ideas object-oriented programming language

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 08:50:421302browse

In PHP object-oriented programming, you will always encounter

class test{
 public static function test(){
  self::func();

  static::func();
 }

 public static function func(){}
}

But do you know the difference between self and static?

In fact, the difference is very simple, you only need to write a few demos to understand:

Demo for self:

class Car
{
 public static function model(){
  self::getModel();
 }

 protected static function getModel(){
  echo "This is a car model";
 }
}

Car::model();

Class Taxi extends Car
{
 protected static function getModel(){
  echo "This is a Taxi model";
 }
}

Taxi::model();
get From the output

This is a car model
This is a car model

, we can see that self will still call the parent class’s method in the subclass. What is called is the method of the parent class, but the method called in the parent class method will also be the method of the subclass (so confusing...)

Before the PHP5.3 version, there was still a little difference between static and self. What is it specifically? , after all, it’s all version 7’s world. I won’t understand it anymore.

The summary is: self can only refer to methods in the current class, and the static keyword allows functions to dynamically bind methods in the class at runtime.

The above introduces the difference between self and static in PHP object-oriented programming, including object-oriented programming and static content. I hope it will be helpful to friends who are interested in PHP tutorials.

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