Home  >  Article  >  Backend Development  >  The use of self,$this,const,static,-> in php_PHP tutorial

The use of self,$this,const,static,-> in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:55:41690browse

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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn