Analysis of self, static and parent keywords in PHP object-oriented programming

This article mainly introduces the use of self, static and parent keywords in PHP object-oriented programming, and analyzes the functions, application scenarios and related use skills of self, static and parent keywords in combination with the form of examples. You can refer to the following examples for your friends, this article describes the use of self, static and parent keywords in PHP object-oriented programming. Share them with you for your reference The details are as follows:
You can see that php contains content about late static binding. Although you don't fully understand it, you can also gain a lot.
When there is no inheritance, the meaning of no inheritance is to write a separate class for use. There is no difference between self and static in the use of the range resolution operator (::).
In static functions, self and static can call static properties and static functions (there is no instantiation class, so non static properties and functions cannot be called).
In non static functions, self and static can call static properties and static functions as well as non static functions
At this point, self and static behave the same, which can be called by replacing the class name.

<?php
class Demo{
 public static $static;
 public $Nostatic; 
 public function __construct(){
  self::$static = "static";
  $this->Nostatic = "Nostatic";
 }
 public static function get(){
  return __CLASS__;
 }
 public function show(){
  return "this is function show with ".$this->Nostatic;
 }
 public function test(){
  echo Demo::$static."<br/>"; //Calling static properties with class name
  echo Demo::get()."<br/>"; //Calling static properties with class name
  echo Demo::show()."<br/>"; //Calling static properties with class name
  echo self::$static."<br/>"; //self calls static properties
  echo self::show()."<br/>"; //self calls non static methods
  echo self::get()."<br/>"; //self calls static methods
  echo static::$static."<br/>";//Static calls static properties
  echo static::show()."<br/>";//Static calls non static methods
  echo static::get()."<br/>"; //Static calls static methods
 }
}
$obj = new Demo();
$obj->test();

Output results;

static
Demo
this is function show with Nostatic
static
this is function show with Nostatic
Demo
static
this is function show with Nostatic
Demo

At the time of inheritance
In inheritance, self and static differ in the use of the range resolution operator (::). parent is also used for inheritance.

<?php
class A{
 static function getClassName(){
  return "this is class A";
 }
 static function testSelf(){
  echo self::getClassName();
 }
 static function testStatic(){
  echo static::getClassName();
 }
}
class B extends A{
 static function getClassName(){
  return "this is class B";
 }
}
B::testSelf();
echo "<br/>";
B::testStatic();

Output results:

this is class A
this is class B

The static method or property called by self always represents the method or property of the current class (A) when it is used, which can be replaced by its class name. However, in the case of long class name or possible change, the way of using self:: is undoubtedly A better choice.
The static method or property called by static will be overridden by its subclass override in inheritance, and should be replaced with the corresponding subclass name (B).
The parent keyword is used to call methods and properties of the parent class. In static methods, you can call the static methods and properties of the parent class; in non static methods, you can call the methods and properties of the parent class.

<?php
class A{
 public static $static;
 public $Nostatic; 
 public function __construct(){
  self::$static = "static";
  $this->Nostatic = "Nostatic";
 }
 public static function staticFun(){
  return self::$static;
 }
 public function noStaticFun(){
  return "this is function show with ".$this->Nostatic;
 }
}
class B extends A{
 static function testS(){
  echo parent::staticFun();
 }
 function testNoS(){
  echo parent::noStaticFun();
 }
}
$obj = new B();
$obj->testS();
echo "<br/>";
$obj->testNoS();

Output results;

static
this is function show with Nostatic

At the end of the article, we analyze an example in the manual

<?php
class A {
 public static function foo() {
  static::who();
 }
 public static function who() {
  echo __CLASS__."\n";
 }
}
class B extends A {
 public static function test() {
  A::foo();
  parent::foo();
  self::foo();
 }
 public static function who() {
  echo __CLASS__."\n";
 }
}
class C extends B {
 public static function who() {
  echo __CLASS__."\n";
 }
}
C::test();
?>

Output result

A
C
C

We separately take out the test method for analysis:

public static function test() {
  A::foo();
  parent::foo();
  self::foo();
}

1) A::foo(); this statement can be executed anywhere. It means to use a to call the static method foo() to get 'a'.
2) The parent:: foo(); the parent of C is B, the parent of B is A, and the foo method of A is found by backtracking; static::who(); the method called by static:: in the statement will be overridden by the subclass, so the who() method of C will be called preferentially. If the WHO method of C does not exist, the WHO method of B will be called, and if the WHO method of B does not exist, the WHO method of A will be called. So the output is' C '. [note 1]
3) self::foo(); this self:: is used in B, so self:: is equivalent to B::, but B does not implement the foo method, and B inherits from a, so we actually call the A::foo() method. The foo method uses the static::who() statement, which causes us to call the who function of C again. [note 2]

<?php
class A {
 public static function foo() {
  static::who();
 }
 public static function who() {
  echo __CLASS__."\n";
 }
}
class B extends A {
 public static function test() {
  A::foo();
  parent::foo();
  self::foo();
 }
 public static function who() {
  echo __CLASS__."\n";
 }
}
class C extends B {
 // public static function who() {
 //  echo __CLASS__."\n";
 // }
}
C::test();
?>

Output results:

A B B

Note 2: supplementary explanation (3) above

<?php
class A {
 public static function foo() {
  static::who();
 }
 public static function who() {
  echo __CLASS__."\n";
 }
}
class B extends A {
 public static function test() {
  A::foo();
  parent::foo();
  self::foo();
 }
 public static function foo() {
  echo "fooB"."\n";
 }
 public static function who() {
  echo __CLASS__."\n";
 }
}
class C extends B {
 public static function foo() {
  echo "fooC"."\n";
 }
 public static function who() {
  echo __CLASS__."\n";
 }
}
C::test();
?>

Output results:

A C fooB

Keywords: Programming PHP

Added by unstable_geek on Thu, 26 Dec 2019 10:23:25 +0200