Home >Backend Development >PHP Problem >Detailed introduction to static methods in PHP
In PHP, static method is a special type of method, which is very different from ordinary methods. This article will introduce in detail the concept, usage and precautions of static methods in PHP.
Concept
In PHP, a static method refers to a method that can be called without instantiating an object. In layman's terms, it is an overall operation on the class, rather than an operation on the instantiated object. Use the static keyword flag in front of the method to tell the PHP interpreter that this is a static method.
Usage method
There are several things to note when using static methods:
There is no need to instantiate the object and can be called directly through the class name:
class Example { public static function Func(){ echo "This is a static function!\n" } } Example::Func(); //Output: This is a static function!
Usually in ordinary methods, we use $ The this keyword is used to reference properties and methods in the class, but in static methods, since there is no instantiated object, the $this keyword cannot be used:
class Example { public $name = "example"; public static function Func(){ echo "This is a static function! \n"; //不能使用$this //echo $this->name; //错误! } }
You can use the self and static keywords in static methods to reference the class itself and the parent class:
class Example { public static function Func(){ echo "This is a static function! \n"; //使用self引用类本身 echo "The class name is: ".self::class."\n"; } } class ChildExample extends Example { public static function Func() { //使用parent关键字引用父类 parent::Func(); //使用static关键字引用当前类 echo "The class name is: ".static::class."\n"; } } ChildExample::Func(); //Output: //This is a static function! //The class name is: Example //The class name is: ChildExample
Static methods can be inherited and overridden like ordinary methods:
class Example { public static function Func(){ echo "This is a static function in Example! \n"; } } class ChildExample extends Example { public static function Func() { echo "This is a static function in ChildExample! \n"; parent::Func(); } } ChildExample::Func(); //Output: //This is a static function in ChildExample! //This is a static function in Example!
Notes
Since static methods do not Instantiated object, so non-static properties cannot be accessed:
class Example { public $name = "example"; public static function Func() { echo "The class name is: ".self::class."\n"; //不能访问非静态属性 //echo "The name is: ".$this->name."\n"; //错误! } } Example::Func(); //The class name is: Example
In PHP, static methods cannot be overridden by non-static methods Override, and non-static methods cannot be overridden. This is because static methods belong to the entire class, not an object, and cannot be polymorphic.
Although static methods can provide a lot of convenience, excessive use of static methods is not conducive to the maintainability and scalability of the program. Therefore, we should try to avoid excessive use of static methods in large projects.
Summary
Static method is a special type of method in PHP, which can be called directly without instantiating the object. Static methods can use the self and static keywords to reference the class itself and the parent class, but cannot use the $this keyword. Since static methods belong to the entire class rather than an object, they should be used as little as possible to ensure program maintainability and scalability.
The above is the detailed content of Detailed introduction to static methods in PHP. For more information, please follow other related articles on the PHP Chinese website!