After using PHP for so long, I feel ashamed that I still haven’t figured out how to use these keywords. Let’s summarize today.
1. When the internal method of a class accesses attributes that have been declared as const and static, use the form of self::$name. Note that the declaration format of the const attribute is const PI=3.14, not const $PI=3.14
以下为引用的内容: class clss_a { private static $name="static class_a"; const PI=3.14; public $value; public static function getName() { return self::$name; } //这种写法有误,静态方法不能访问非静态属性 public static function getName2() { return self::$value; } public function getPI() { return self::PI; } } |
The following is the quoted content: class clss_a { private static $name="static class_a" ; const PI=3.14; public $value; public static function getName() { return self::$name; } //This kind Wrong writing, static methods cannot access non-static properties public static function getName2() { return self::$value; } public function getPI() { return self::PI; }
}
|
Another thing to note is that if a class method is static, the properties it accesses must also be static.
2. When the internal method of a class accesses attributes that are not declared const or static, use the form $this->value ='class_a';.
http://www.bkjia.com/PHPjc/364393.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364393.htmlTechArticleAfter using PHP for so long, I feel ashamed that I still haven’t figured out how to use these keywords. . Let’s summarize today. 1. The internal method access of the class has been declared as const and sta...