You cannot use this to call non-member functions within a static member function, but you can use self to call static member functions/variables/constants;
Other member functions can be called with self Static member functions and non-static member functions.
As the discussion deepened, I found that self is not that simple. In view of this, this article first compares and differentiates several keywords, and then summarizes the usage of self.
Differences from parent, static and this
If you want to fully understand self, you must distinguish it from parent, static and this.
You cannot use this to call non-member functions within a static member function, but you can use self to call static member functions/variables/constants;
Other member functions can use self to call static member functions and non-static functions member functions.
As the discussion deepened, I found that self is not that simple. In view of this, this article first compares and differentiates several keywords, and then summarizes the usage of self.
Differences from parent, static and this
If you want to fully understand self, you must distinguish it from parent, static and this.
class Base { public function __construct() { echo "Base contructor!", PHP_EOL; } } class Child { public function __construct() { parent::__construct(); echo "Child contructor!", PHP_EOL; } } new Child; // 输出: // Base contructor! // Child contructor!
static
The general purpose of static is to modify a function or variable to make it a class function and class variable. It can also modify variables within a function to extend its life cycle to the life cycle of the entire application.
But its association with self is a new use introduced since PHP 5.3: static delayed binding.
With the static delayed binding function of static, the belonging class can be dynamically determined at runtime.
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 terms of function references, the difference between self and static is: for static member functions, self points to the current class of the code, static points to the calling class; for non-static member functions, self suppresses polymorphism and points to the member function of the current class. static is equivalent to this, and dynamic points to the function of the calling class.
The three keywords parent, self, and static are very interesting when combined together. They point to the parent class, current class, and subclass respectively, which has a bit of a "past, present, and future" flavor.
this
self and this are the most discussed combinations and are also the most likely to be misused.
The main differences between the two are as follows:
this cannot be used in static member functions, self can;
For static member functions/variables For access, it is recommended to use self instead of $this:: or $this->;
For access to non-static member variables, self cannot be used, only this;
this should be used when the object has been instantiated, self does not have this restriction;
is used in non-static member functions, self suppresses polymorphic behavior and refers to the function of the current class; while this refers to the calling class's heavy function Override the function (if any).
The purpose of self
After reading the differences between the three keywords mentioned above, is the purpose of self immediately apparent? To sum up in one sentence, that is: self always points to "the current class (and class instance)".
In detail:
Replace the class name and refer to the static member variables and static functions of the current class;
Suppress polymorphic behavior and refer to the functions of the current class instead of Implementation overridden in subclasses;
slot
这几个关键字中,只有this要加$符号且必须加,强迫症表示很难受;
静态成员函数中不能通过$this->调用非静态成员函数,但是可以通过self::调用,且在调用函数中未使用$this->的情况下还能顺畅运行。此行为貌似在不同PHP版本中表现不同,在当前的7.3中ok;
在静态函数和非静态函数中输出self,猜猜结果是什么?都是string(4) "self",迷之输出;
return $this instanceof static::class;会有语法错误,但是以下两种写法就正常:
$class = static::class; return $this instanceof $class; // 或者这样: return $this instanceof static;
所以这是为什么啊?!
$class = static::class;
return $this instanceof $class;
// 或者这样:
return $this instanceof static;
以上就是解析PHP的self关键字的详细内容。
The above is the detailed content of Get PHP's self keyword in minutes. For more information, please follow other related articles on the PHP Chinese website!