views:

118

answers:

2

I ran into a very strange behaviour in some of our PHP code today. We have a class for dealing with files. It's something like this:

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;
  }
}

It looked pretty normal to us, but the $this->upload() function never returned anything but NULL. We checked that the correct function was running. We echoed out its return value before it returned. We tried only returning a true value or even a string. Everything was checking out right. But $this->upload still evaluated to NULL. Also, there was nothing in the logs and ERROR_ALL is on.

In exasperation we changed the function name to foo_upload. All of a sudden everything worked. "upload" is not in the list of PHP reserved words. Anyone have any thoughts why a class function named "upload" would fail?

+1  A: 

One way to get null when "calling" upload would be if you had this (trying to access an inexisting property) :

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

instead of this (from OP) (trying to call an existing method):

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

Did you check you didn't forget the () ?

If it's not this, try with error_reporting set to E_ALL, and displaying the errors :

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

*(you said "ERROR_ALL is on", so not sure it's what you meant)*

Pascal MARTIN
sorry, I meant error_reporting(E_ALL). The parentheses are present and there are no object vars with the same name. We did try catching the result of $this->upload() in a $var as you suggest. It didn't make a difference.
dnagirl
ok :-( really interested to know the answer, then !
Pascal MARTIN
+2  A: 

Make sure the return statement at the end of the upload method is the only return statement in that method.

Horia Dragomir
It is. But that's a very good point.
dnagirl