Detailed Explanation of self Keyword in PHP

  • 2021-12-12 08:05:36
  • OfStack

Preface

Some people in PHP group ask about the usage of self keyword, and the answer is obvious: non-member functions cannot be called with this in static member functions, but static member functions/variables/constants can be called with self; Other member functions can use self to call static member functions as well as non-static member functions. With the deepening of the discussion, it is found that self is not that simple. In view of this, this article first compares and distinguishes several keywords, and then summarizes the usage of self.

Differences from parent, static and this

If you want to understand self thoroughly, you should distinguish it from parent, static and this. The following are compared separately.

parent

self is easy to distinguish from parent: parent refers to methods (or variables) whose parent/base class is implicitly covered, while self refers to its own methods (or variables). For example, calling the parent class constructor in the constructor:


class Base {
 public function __construct() {
  echo "Base contructor!", PHP_EOL;
 }
}

class Child {
 public function __construct() {
  parent::__construct();
  echo "Child contructor!", PHP_EOL;
 }
}

new Child;
//  Output: 
// Base contructor!
// Child contructor!

static

The general purpose of static is to modify a function or variable to become a class function and class variable, or to modify variables within a function to extend its life cycle to the whole life cycle of an application. But its association with self is a new use introduced since PHP 5.3: static delayed binding.

With static's static deferred binding capability, the attributable class can be dynamically determined at run time. For example:


class Base {
 public function __construct() {
  echo "Base constructor!", PHP_EOL;
 }

 public static function getSelf() {
  return new self();
 }

 public static function getInstance() {
  return new static();
 }

 public function selfFoo() {
  return self::foo();
 }

 public function staticFoo() {
  return static::foo();
 }

 public function thisFoo() {
  return $this->foo();
 }

 public function foo() {
  echo "Base Foo!", PHP_EOL;
 }
}

class Child extends Base {
 public function __construct() {
  echo "Child constructor!", PHP_EOL;
 }

 public function foo() {
  echo "Child Foo!", PHP_EOL;
 }
}

$base = Child::getSelf();
$child = Child::getInstance();

$child->selfFoo();
$child->staticFoo();
$child->thisFoo();

The program output is as follows:

Base constructor!
Child constructor!
Base Foo!
Child Foo!
Child Foo!

In function reference, the difference between self and static is that for static member functions, self points to the current class of the code and static points to the calling class; For non-static member functions, self suppresses polymorphism and points to the member function of the current class, while static is equivalent to this and dynamically points to the function of the calling class.

The three keywords parent, self and static are interesting to see together in one case, pointing to parent class, current class and subclass respectively, which is a bit of "past, present and future".

this

self and this are the most discussed and most likely to be misused combinations. The main differences between the two are as follows:

this cannot be used in static member functions, self can; For access to static member functions/variables, self is recommended, not $this:: or $this- > The form of; For access to non-static member variables, self cannot be used, only this can be used; this should be used if the object has been instantiated, self does not have this restriction; Used in non-static member functions, self suppresses polymorphic behavior and refers to functions of the current class; this refers to the override (override) function of the calling class, if any.

Use of self

After reading the difference with the above three keywords, is self used at once? To sum up in one sentence, self always points to "current class (and class instance)". In detail, it is:

Alternative class name, referring to static member variables and static functions of the current class; Suppress polymorphic behavior and refer to the functions of the current class instead of the implementation of coverage in subclasses;

Groove point

Among these keywords, only this should be added with $symbol and must be added, and obsessive-compulsive disorder means very uncomfortable; You cannot pass $this- > Call a non-static member function, but can be called through self:: and $this is not used in the calling function- > It can run smoothly under the condition of. This behavior appears to behave differently in different versions of PHP, and in the current 7.3 ok; Output self in static functions and non-static functions. Guess what the result is? They are all string (4) "self", the output of fans; return $this instanceof static:: class; There will be grammatical errors, but the following two ways of writing are normal:

$class = static::class;
return $this instanceof $class;
//  Or this: 
return $this instanceof static;

So why is this? !

Reference

When to use self over $this?

Summarize


Related articles: