我今天在一些PHP代码中遇到了一个非常奇怪的行为.我们有一个处理文件的类.它是这样的:
class AFile {
//usual constructor, set and get functions, etc.
//...
public function save() {
//do some validation
//...
if($this->upload()) { //save the file to disk
$this->update_db(); //never reached this line
}
}
private function upload() {
//save the file to disk
//...
return ($success) ? true : false;
}
}
Run Code Online (Sandbox Code Playgroud)
它看起来很正常,但是$ this-> upload()函数永远不会返回NULL.我们检查了正确的函数是否正在运行.我们在返回之前回复了它的返回值.我们只尝试返回一个真值或一个字符串.一切都正常.但$ this-> upload仍然评估为NULL.此外,日志中没有任何内容,ERROR_ALL已打开.
在愤怒中,我们将函数名称更改为foo_upload.突然间一切都奏效了."upload"不在PHP保留字列表中.任何人都有任何想法为什么名为"上传"的类函数会失败?
当“调用”上传时获取 null 的一种方法是如果您有这个(尝试访问不存在的属性):
if($a = $this->upload) { // => NULL
$this->update_db(); //never reached this line
}
var_dump($a);
Run Code Online (Sandbox Code Playgroud)
而不是这个(来自OP)(尝试调用现有方法):
if($a = $this->upload()) { // => true or false
$this->update_db(); //never reached this line
}
var_dump($a);
Run Code Online (Sandbox Code Playgroud)
你检查过你没有忘记吗()?
如果不是这样,请尝试将 error_reporting 设置为E_ALL,并显示错误:
ini_set('display_errors', true);
error_reporting(E_ALL);
Run Code Online (Sandbox Code Playgroud)
(你说“ERROR_ALL is on”,所以不确定这就是你的意思)