5

我今天在我们的一些 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;
  }
}

它对我们来说看起来很正常,但是 $this->upload() 函数只返回 NULL 之外的任何内容。我们检查了正确的功能正在运行。我们在它返回之前回显了它的返回值。我们尝试只返回一个真值,甚至是一个字符串。一切都检查正确。但是 $this->upload 仍然评估为 NULL。此外,日志中没有任何内容,并且 ERROR_ALL 已打开。

恼怒的是,我们将函数名称更改为 foo_upload。突然间,一切都奏效了。“上传”不在PHP 保留字列表中。任何人有任何想法为什么名为“上传”的类函数会失败?

4

2 回答 2

2

确保上传方法末尾的 return 语句是该方法中唯一的 return 语句。

于 2009-07-24T11:09:46.360 回答
1

“调用”上传时获取 null 的一种方法是,如果您有这个(尝试访问不存在的属性):

if($a = $this->upload) { // => NULL
  $this->update_db(); //never reached this line
}
var_dump($a);

而不是这个(来自OP)(试图调用现有方法):

if($a = $this->upload()) { // => true or false
  $this->update_db(); //never reached this line
}
var_dump($a);

你检查你没有忘记()吗?

如果不是这样,请尝试将 error_reporting 设置为E_ALL,并显示错误:

ini_set('display_errors', true);
error_reporting(E_ALL);

(您说“ERROR_ALL 已开启”,所以不确定您的意思)

于 2009-07-23T17:23:27.397 回答