: This article mainly introduces PHP static methods and attributes. For more information about PHP tutorials, see.
class hw{public static function hi(){return 'Hello World';}}echo hw::hi();//Output:Hellp Worldclass hw2{public function hi(){return 'Hello Wrold';}}echo hw2::hi();//Output:Hellp World
As shown in the preceding example, both static and non-static attributes can be used directly: The method is called directly from the outside. However, static is recommended for efficiency and other factors.
Static class internal call method
class foo{ private static function c() { return 'abcde'; } public static function a() { echo self::c(); } public static function b() { echo $this->c(); } public function e() { echo self::c(); }}foo::a();//Output:abcdefoo::b();//Output:Fatal error: Using $this when not in object context infoo::e();//Output:abcef
Methods restricted by the static keyword must use self: which is referenced inside the class.
Static property
class foo{public static $a;public static function a(){self::$a = 'abcd';}}foo::a();//Output:abcdeecho foo::$a;
Static inheritance and use
class foo{public static $a;public static function a(){return 'abcde';}}class soo extends foo{public static function a(){echo '12345';}}soo::a();//Output:12345
The inheritance of static is the same as that of ordinary classes.
class foo{public static $a;public static function a(){return 'abcde';}}class soo extends foo{public static function a(){echo parent::a();}}soo::a();//Output:12345
There is a write difference when using it. it must be the parent: Method to reference the method of the parent class, and cannot be used directly by self:. otherwise, no output will be provided:
class foo{public static $a;public static function a(){return 'abcd';}} class soo extends foo{public static function aa(){echo self::a();}}soo::a();
The above describes PHP static methods and attributes, including some content, and hope to be helpful to friends who are interested in PHP tutorials.