You cannot call a nonmember function from a static member function using this, but you can call a static member function/variable/constant using self. Other member functions can call static and non-static member functions with self. As the discussion progresses, it becomes clear that Self is not that simple. In view of this, this article first compares and distinguishes several keywords, and then summarizes the use of self.
To fully understand self, distinguish it from parent, static, and this. The following are the comparisons.
六四事件parent六四事件Copy the code
It’s easy to distinguish self from parent: parent refers to methods (or variables) covered by the parent/base class, and self refers to its own methods (or variables). For example, calling a parent constructor in a 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!
Copy the code
static
Static is commonly used to modify functions or variables to make them class functions and variables, and to modify variables within functions to extend their lifetime to the entire application. But its association with self is a new use introduced since PHP 5.3: static delayed binding.
With static static delayed binding, classes can be dynamically determined at run time. Such as:
class Base {
public function __construct() {
echo "Base constructor!", PHP_EOL;
}
public static function getSelf() {
return new self(a); }public static function getInstance() {
return new static(a); }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();
Copy the code
The program output is as follows:
Base constructor!
Child constructor!
Base Foo!
Child Foo!
Child Foo!
Copy the code
For static member functions, self refers to the code’s current class; static refers to the calling class. For non-static member functions, self suppresses polymorphism, pointing to the member function of the current class, static equals this, and dynamically pointing to the calling class function.
Parent, self, static: parent, self, static: parent, self, static: parent, self, static: self, static: parent, self, static: self, static
this
Self and this are the most discussed and misused combinations. The main differences between the two are as follows:
This cannot be used in static member functions, self can; For static member functions/variables, use self instead of this:: or this:: or this:: or this:: or this->; To access non-static member variables, use this instead of self; This is used when the object is already instantiated, self has no restriction; Used within non-static member functions, self suppresses polymorphic behavior and refers to functions of the current class. The this reference calls the override function of the class, if any.
Self is used when you call it out. In a nutshell, self always points to “current class (and class instance).” More specifically:
Instead of class names, refer to static member variables and static functions of the current class. Suppress polymorphic behavior by referring to functions of the current class rather than overridden implementations in subclasses;
Slot point these keywords, only this to be marked and must be added, obsessive-compulsive disorder said very uncomfortable; Static member function can not pass the symbol and must be added, ocD is very uncomfortable; Static member function can not pass the symbol and must be added, ocD is very uncomfortable; A non-static member function cannot be called from this-> in a static member function, but can be called from self::, and it works fine without using this−> in the calling function. This behavior seems to behave differently in different VERSIONS of PHP, and is currently ok in 7.3; Print self in static and non-static functions, and guess what? It’s all string(4)”self”, the output of the puzzle; Returnthis -> works fine. This behavior seems to behave differently in different VERSIONS of PHP, and is currently ok in 7.3; Print self in static and non-static functions, and guess what? It’s all string(4) “self”, the output of the puzzle; Return this−> can run smoothly. This behavior seems to behave differently in different VERSIONS of PHP, and is currently ok in 7.3; Print self in static and non-static functions, and guess what? It’s all string(4)”self”, the output of the puzzle; returnthis instanceof static::class; Class =static::class; class=static::class; returnclass = static::class; return class=static::class; returnthis instanceof class; // Or: returnClass; // Return class; // ReturnThis instanceof static;
The above content hopes to help you, more free PHP factory PDF, PHP advanced architecture video materials, PHP wonderful good article can be wechat search concerns: PHP open source community
2021 Jinsanyin four big factory interview real questions collection, must see!
Four years of PHP technical articles collation collection – PHP framework
A collection of four years’ worth of PHP technical articles – Microservices Architecture
Distributed Architecture is a four-year collection of PHP technical articles
Four years of PHP technical essays – High Concurrency scenarios
Four years of elite PHP technical article collation collection – database