Page 1 of 1

returning object by reference

Posted: Thu Nov 17, 2005 2:48 pm
by Ree
I get the 'Only variable references should be returned by reference' error when running this method:

Code: Select all

function &query($sql)
  {
    $result = mysql_query($sql, $this->connection);
    return new MySQLResultIterator($result);
  }
Does that mean it is not possible to return objects by reference?

Posted: Thu Nov 17, 2005 3:51 pm
by Weirdan
It means you should do it like this:

Code: Select all

function &query($sql)
  {
    $result = mysql_query($sql, $this->connection);
    $ret =&  new MySQLResultIterator($result);
    return $ret;
  }

Posted: Sun Nov 20, 2005 11:29 am
by Ree
Well, yeah, this can be done, but I've noticed similar code elsewhere as well, for example, here: http://www.phppatterns.com/docs/design/ ... ory_method

Code: Select all

class MessageMaker {
    // The factory method...
    function & makeMessage () {
        // Return a new instance of RandomMessage
        return new RandomMessage(); 
    }
}
Or here: http://www.phppatterns.com/docs/develop ... references

Code: Select all

class MessageFactory {
    // Return a reference to an instance of Message
    function & createMessage () {
        return new Message;
    }
}
It seems it shouldn't be a problem? Or are those used in higher versions of PHP (mine is 4.4.0)?

Posted: Sun Nov 20, 2005 11:45 am
by John Cartwright
I think thats for PHP5, as they pass by reference automatically

Posted: Sun Nov 20, 2005 1:47 pm
by lilleman
Jcart wrote:I think thats for PHP5, as they pass by reference automatically
In PHP5, object handles (pointers) are used. This means that you don't have to use references when you want to make changes to an object instance from inside a function or method.

Code: Select all

class Person
{
  private $name;
  
  public function setName($name)
  {
    $this->name = $name;
  }
}

function changeName($person)
{
  $person->setName('John');
}

$person = new Person;
$person->setName('Joe');

changeName($person);

// the name of $person is now 'John', instead of 'Joe'

Posted: Sun Nov 20, 2005 3:04 pm
by Ree
Now if I removed the & before the method name (query instead of &query), it would still return reference, right?

Code: Select all

function query($sql)
  {
    $result = mysql_query($sql, $this->connection);
    $ret =&  new MySQLResultIterator($result);
    return $ret;
  }

Posted: Sun Nov 20, 2005 3:11 pm
by sweatje
Would probably be cleanest as:

Code: Select all

function &query($sql) {
     return $ret =&  new MySQLResultIterator(mysql_query($sql, $this->connection));
   }

Posted: Sun Nov 20, 2005 4:28 pm
by Ree
Yes, that looks nicer. So, I assume, the answer to my last question is 'yes', correct?

Posted: Sun Nov 20, 2005 4:31 pm
by sweatje
Actually the answer is no, if you remove the instruction to return by reference, it is not returned that way.

Edited my first post to add the by ref operator back into the function definition.

Posted: Sun Nov 20, 2005 4:37 pm
by Ree
I see. Thanks for explaining.