const 关键字定义 常量,在定义和使用常量的时候不需要使用 $ 符号。
常量必须是一个定值,不能是其他的变量或者函数式等。
从5.3.0起可以使用一个变量调用一个类,但该变量的值不能是关键字(self,parent,static).
<?php
class MyClass {
const constant ='the constant content';
function showConstant(){
echo self::constant. '\n';
}
}
echo MyClass::constant . '\n';
$classname = 'MyClass';
echo $classname::constant . '\n'; //自版本 5.3.0起
***********************************************************************************************************************************************************
/
***********************************************************************************************************************************************************
static 声明静态变量或者方法;
静态变量或者静态方法可以不实例化类而直接访问,
静态属性不能通过一个已实例化的对象访问,静态方法可以。
因为静态方法不需要对象即可访问,所以伪变量$this 在静态方法中不可用。
***************************************************************************************************************************************************
$this ----------------------> object
self ----------------------->class