我有一个私人功能
private $_CODES = array(
301 => array(
"code"=> 301,
"message"=> "User With ID Not Found",
"type"=> "Error"
),
302 => array(
"code"=> 302,
"message"=> "Email Id Not Valid",
"type"=> "Error"
),
);
使用公共函数getError访问此函数这两个函数属于同一类。
public function getError($code)
{
return $this->_CODES[]=$code;
}
我只收到错误代码,表示301没有收到确切的消息。传递参数($ code)时需要得到相应的错误信息。我错过了什么? 我已经像这样调用了错误函数
echo json_encode(Errors::getError(301));
答案 0 :(得分:3)
public function getError($code)
{
return $this->_CODES[$code]['message'];
}
答案 1 :(得分:2)
return $this->_CODES[]=$code;
该代码将$code
附加到$this->_CODES
数组,然后返回该赋值的结果,即$code
本身。
例如:
$array = array();
echo $array[] = 123; // "123"
print_r($array); // Array(123)
另请参阅:Creating/modifying with square bracket syntax
您将需要使用这样的数组解除引用:
return $this->_CODES[$code]['message']; // return just the message
或者:
return $this->_CODES[$code]; // returns all three fields as a new array
另请参阅:Accessing array elements with square bracket syntax
如果你想要Errors::getError(301)
工作,你需要将你的函数和数组声明为静态,即:
class Errors
private static $_CODES = array(
301 => array(
"code"=> 301,
"message"=> "User With ID Not Found",
"type"=> "Error"
)
);
public static function getError($code) {
return self::$_CODES[$code]['message'];
}
}