PHP使用静态属性和静态方法为API接口提供信息提示!

ErrorMessage.class.php 文件内容:

class ErrorMessage{
    
              static $Error_none = 0;//API请求成功
    public         static $Error_validation = 1; //API请求验证失败
    public         static $Error_register = 2; //API请求注册失败
    public         static $Error_login = 3; //API请求登录失败 
        protected    static $Error_protected = -1;//外部不可访问
        private         static $Error_private = -2;//外部不可访问
 
    
    public static function getErrorMessage( $code = 0 )
    {
        $Error_Message = array(
        self::$Error_none=> "结果正确", 
        self::$Error_validation=> "验证失败", 
        static::$Error_register=> "注册失败", 
        ErrorMessage::$Error_login=> "登录失败",   
        ErrorMessage::$Error_protected=> "Error_protected",   
        ErrorMessage::$Error_private=> "Error_private",   
        
        );      
        return $Error_Message[$code];
    }
} 

调用示例:


echo"\$Error_none : ";           echo ErrorMessage::getErrorMessage(0);echo"
"; echo"\$Error_validation : "; echo ErrorMessage::getErrorMessage(1);echo"
"; echo"\$Error_register : "; echo ErrorMessage::getErrorMessage(2);echo"
"; echo"\$Error_login : "; echo ErrorMessage::getErrorMessage(3);echo"
";

实例化对象调用示例:


$error=new ErrorMessage();
echo"\$Error_none :";          print_R($error->getErrorMessage(0));echo"
"; echo"\$Error_validation :"; print_R($error->getErrorMessage(1));echo"
"; echo"\$Error_register :"; print_R($error->getErrorMessage(2));echo"
"; echo"\$Error_login :"; print_R($error->getErrorMessage(3));echo"
";
PHP使用静态属性和静态方法为API接口提供信息提示!_第1张图片
Paste_Image.png

继承父类:

class Err extends ErrorMessage{ 
   
    public    static $Error_protecteds = -1;  
    public    static $Error_privates = -2;  
    
    static public function getErr($code = 0){
        return parent::getErrorMessage($code);
        
    }
}

调用示例:


//echo Err::$Error_protected;echo"
";//调用父类受保护属性会报错 //echo Err::$Error_private;echo"
"; //调用父类私有属性会报错 echo Err::$Error_protecteds;echo"
"; echo Err::$Error_privates;echo"
"; echo Err::getErr(0);echo"
"; echo Err::getErr(1);echo"
"; echo Err::getErr(2);echo"
"; echo Err::getErr(3);echo"
"; echo Err::getErr(-1);echo"
"; echo Err::getErr(-2);echo"
";
PHP使用静态属性和静态方法为API接口提供信息提示!_第2张图片
Paste_Image.png

你可能感兴趣的:(PHP使用静态属性和静态方法为API接口提供信息提示!)