Usage Analysis of self static and parent Keywords in PHP Object Oriented Programming

  • 2021-12-13 16:35:28
  • OfStack

This article describes the example of PHP object-oriented programming self, static, parent keyword usage. Share for your reference, as follows:

See php inside about the content of late static binding, although not fully understand, but also harvest a lot of things.

Introduction to php official manual:
http://php.net/manual/zh/language.oop5.late-static-bindings.php

When there is no inheritance

There is no inheritance, that is, when writing a separate class to 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 (non-static properties and functions cannot be called because there are no instantiated classes). Among 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 like 1 and can be called with the class name:: instead.


<?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/>"; // Invoking static properties using class names 
  echo Demo::get()."<br/>"; // Invoking static properties using class names 
  echo Demo::show()."<br/>"; // Invoking static properties using class names 
  echo self::$static."<br/>"; //self Invoke static properties 
  echo self::show()."<br/>"; //self Call a non-static method 
  echo self::get()."<br/>"; //self Call a static method 
  echo static::$static."<br/>";//static Invoke static properties 
  echo static::show()."<br/>";//static Call a non-static method 
  echo static::get()."<br/>"; //static Call a static method 
 }
}
$obj = new Demo();
$obj->test();

Output:

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

When inheriting

When inheriting, self and static differ in the use of the scope 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:

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) at the time it is used, and can be replaced by its class name, but in cases where the class name is long or may change, it is undoubtedly a better choice to use self::.

Static methods or properties called by static are overridden by their subclass overrides in inheritance and should be replaced with the corresponding subclass name (B).

The parent keyword is used to call the 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 result

static
this is function show with Nostatic

At the end of the article, we analyze an example from a 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

Let's take out the test method for analysis:


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

1) A::foo(); This statement, which can be executed anywhere, means using A to call the static method foo () to get 'A'.

2) parent::foo(); parent of C is B, parent of B is A, and foo method of A is found backwards. static::who(); The method called by static:: in the statement will be overwritten by subclasses, so the who () method of C will be called first, the who method of B will be called if the who method of C does not exist, and the who method of A will be called if the who method of B does not exist. Therefore, 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 A::foo() This method. The foo method uses the static::who() Statement, causing us to call the who function of C again. [Note 2]

Note 1: Supplementary explanation (2) 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 who() {
  echo __CLASS__."\n";
 }
}
class C extends B {
 // public static function who() {
 //  echo __CLASS__."\n";
 // }
}
C::test();
?>

Output:

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:

A C fooB

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 Grammar", "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: