returning object by reference

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

returning object by reference

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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;
  }
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post 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)?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I think thats for PHP5, as they pass by reference automatically
lilleman
Forum Newbie
Posts: 7
Joined: Thu Nov 17, 2005 4:55 pm
Location: Örebro, Sweden

Post 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'
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post 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;
  }
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

Would probably be cleanest as:

Code: Select all

function &query($sql) {
     return $ret =&  new MySQLResultIterator(mysql_query($sql, $this->connection));
   }
Last edited by sweatje on Sun Nov 20, 2005 4:31 pm, edited 1 time in total.
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

Yes, that looks nicer. So, I assume, the answer to my last question is 'yes', correct?
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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.
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

I see. Thanks for explaining.
Post Reply