PHP:在class中定义常量注意事项

一、不能在成员函数中定义常量,否则会引发诡异地语法错误 syntax error, unexpected 'CONST' (T_CONST)

示例

/* 错误的方式 */
class  A
{
     public function myfunction()
     {
           const CONST_VAR = 0; 
     }   
}


/* 正确的方式 */
class  A
{
     const CONST_VAR = 0; 
     public function myfunction()
    {
           echo self::CONST_VAR;
           
    } 
}
    

 

二、不能使用public  protected private  static等修饰符。

示例

 

/* 错误的方式 */
class  A
{
     public static const  CONST_VAR;
     private const CONST_VAR2;
}


/* 正确的方式 */
class  A
{
    const  CONST_VAR;
    const CONST_VAR2;
}

 

posted @ 2015-10-23 15:51  Alexander.Gao  阅读(1015)  评论(0编辑  收藏  举报