2009-07-23 47 views
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 reserved words的列表中。任何人有任何想法,为什么名为“上传”的类功能会失败?

+0

你把上传功能里面的回声? – 2009-07-23 16:43:02

+0

它可能被重写的地方? – Greg 2009-07-23 16:47:52

+0

@Chacha:我们确实在上传函数中放入了一个回声。它给出了预期值。 @Greg:AFile类没有子类(尚未),所以它的方法不应该被覆盖。 – dnagirl 2009-07-23 16:58:15

回答

1

一种方式来获得空当“呼吁”上传是,如果你有这个(试图访问一个inexisting属性):

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是”,所以不知道这是你的意思)

2

确保上传方法结束时的return语句是该方法中唯一的return语句。