Home  >  Article  >  Backend Development  >  php self,$this,const,static,->的使用_PHP教程

php self,$this,const,static,->的使用_PHP教程

WBOY
WBOYOriginal
2016-07-21 15:43:23992browse

今天来总结下。
、在类的内部方法访问已经声明为const及static的属性时,使用self::$name的形式。注意的是const属性的申明格式,const PI=3.14,而不是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;
}


}

还要注意的一点是如果类的方法是static的,他所访问的属性也必须是static的。
、在类的内部方法访问未声明为const及static的属性时,使用$this->value ='class_a';的形式。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/320765.htmlTechArticle今天来总结下。 、在类的内部方法访问已经声明为const及static的属性时,使用self::$name的形式。注意的是const属性的申明格式,const PI=3.14,而...
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