class hw { public static function hi() { return 'Hello World'; } } echo hw::hi();//Output:Hellp World class hw2 { public function hi() { return 'Hello Wrold'; } } echo hw2::hi();//Output:Hellp World
As can be seen from the above example, after using the static attribute or not using the static attribute, you can directly use the :: method to call it directly from the outside. However, for efficiency and other reasons, it is recommended to use static restrictions.
Calling methods within a static class
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:abcde foo::b();//Output:Fatal error: Using $this when not in object context in foo::e();//Output:abcef
static attribute
class foo { public static $a; public static function a() { self::$a = 'abcd'; } } foo::a();//Output:abcde echo 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
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
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 introduces PHP static methods and attributes, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.