Home > Backend Development > PHP Tutorial > php self,$this,const,static,->的使用_php技巧

php self,$this,const,static,->的使用_php技巧

WBOY
Release: 2016-05-17 09:29:54
Original
814 people have browsed it

今天来总结下。
、在类的内部方法访问已经声明为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';的形式。
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template