By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,035 Members | 1,471 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,035 IT Pros & Developers. It's quick & easy.

pass objects by reference (PHP4/5)

P: n/a
(apologies if this message is a duplicate -- my news server seems to
have problems)

Greetings,

When using PHP 4, this:

// ex. 1
class A {
function A(&$obj) {
$this->object =& $obj; // =& not needed?
}
}

is one reference too many: it should be

// ex. 2
class A {
function A(&$obj) {
$this->object = $obj; // = instead of =&
}
}

because $obj is already a reference when it enters the constructor, is
that correct?

Further question: when I upgrade to PHP 5, should I remove the & in
constructor to get the same effect? Objects are automatically passed by
reference in PHP 5, did I get that correctly? So the PHP 5 version would be:

// ex. 3
class A {
function A($obj) { // automatically passed by reference?
$this->object = $obj; // but what happens here?
}
}

But what happens in the $this->object = $obj; line in PHP 5? Is
$this->obj now a reference to a reference, as in ex. 1, assuming I
understood that piece of code correctly? If so, how do I get the ex. 2
behaviour in PHP 5?

Thanks,
Jan Pieter Kunst

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Tue, 21 Dec 2004 06:23:34 +0100, Jan Pieter Kunst <jp*@akamail.com> wrote:
(apologies if this message is a duplicate -- my news server seems to
have problems)

Greetings,

When using PHP 4, this:

// ex. 1
class A {
function A(&$obj) {
$this->object =& $obj; // =& not needed?
}
}

is one reference too many: it should be

// ex. 2
class A {
function A(&$obj) {
$this->object = $obj; // = instead of =&
}
}

because $obj is already a reference when it enters the constructor, is
that correct?
No, if the intention is to assign a reference to $this->object; $obj is set to
a reference to the caller's parameter, but then if you want to assign it to
something else as a reference, you'd need to again use =& else you'll be
assigning a copy by value.
Further question: when I upgrade to PHP 5, should I remove the & in
constructor to get the same effect? Objects are automatically passed by
reference in PHP 5, did I get that correctly? So the PHP 5 version would be:

// ex. 3
class A {
function A($obj) { // automatically passed by reference?
$this->object = $obj; // but what happens here?
}
}

But what happens in the $this->object = $obj; line in PHP 5? Is
$this->obj now a reference to a reference, as in ex. 1, assuming I
understood that piece of code correctly? If so, how do I get the ex. 2
behaviour in PHP 5?


It's my understanding that rather than objects being passed by reference as
such, all object variables are in PHP5 now represented by IDs which PHP
dereferences internally. You can then pass these IDs by value or by reference,
it doesn't really matter, since when any methods get invoked, it's dereferenced
to the same thing.

So, PHP4 code stuffed with '&' referencing operators will work the same in
PHP5 (as you're now passing object IDs by reference, which is equivalent to
passing object IDs by value since you can't modify the IDs). You could remove
the &'s if you start using PHP5-only functionality and so have stopped caring
about PHP4 compatibility.

If you var_dump() and then print() an object in PHP4 you get something like:

object(wibble)(2) {
["wobble"]=>
NULL
["wibble"]=>
string(6) "wobble"
}
Object

In PHP5 you get the following; note the #1 on the end - this is the object ID:

object(wibble)#1 (2) {
["wobble"]=>
NULL
["wibble"]=>
string(6) "wobble"
}
Object id #1

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2

P: n/a
.oO(Jan Pieter Kunst)
When using PHP 4, this:

// ex. 1
class A {
function A(&$obj) {
$this->object =& $obj; // =& not needed?
Needed.
is one reference too many: it should be

// ex. 2
class A {
function A(&$obj) {
$this->object = $obj; // = instead of =&
}
}

because $obj is already a reference when it enters the constructor, is
that correct?
Nope. It doesn't matter if the RHS (right hand side) of the assignment
operator is a reference or not, if used without the reference sign the
operator will assign a copy(!) to the LHS. Watch this:

$foo = new TFoo();

Now $foo doesn't contain the object that was created actually, but a
copy of it (this becomes important if you create other objects inside
the constructor and pass a self-reference to them)!

Whenever you want to work with references in PHP4 you have to use the
reference sign, always. Omitting it once will break the chain.
Further question: when I upgrade to PHP 5, should I remove the & in
constructor to get the same effect? Objects are automatically passed by
reference in PHP 5, did I get that correctly?
Yep.
So the PHP 5 version would be:

// ex. 3
class A {
function A($obj) { // automatically passed by reference?
$this->object = $obj; // but what happens here?
}
}
Works as intended, but nevertheless in PHP5 you should use the new
generic constructor and declare member variables, e.g.

class A {
private $object;

public function __construct($obj) {
$this->object = $obj;
}
}
But what happens in the $this->object = $obj; line in PHP 5? Is
$this->obj now a reference to a reference, as in ex. 1, assuming I
understood that piece of code correctly?


Nope. References are no pointers! They are just a kind of alias name.
Pointers to pointers exist, but a reference is always just that: an
alias name for a kind of object. There's no reference to a reference.

HTH
Micha
Jul 17 '05 #3

P: n/a
Michael Fesser wrote:
.oO(Jan Pieter Kunst)
But what happens in the $this->object = $obj; line in PHP 5? Is
$this->obj now a reference to a reference, as in ex. 1, assuming I
understood that piece of code correctly?

Nope. References are no pointers! They are just a kind of alias name.
Pointers to pointers exist, but a reference is always just that: an
alias name for a kind of object. There's no reference to a reference.

A-ha! Now things are getting clear. I was under the impression that
references are like pointers. OK, thanks very much for clearing that up.
Time to re-check the &'s in my PHP 4 code.

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #4

P: n/a
"Jan Pieter Kunst" <de*****@cauce.org> wrote in message
news:41*********************@news.xs4all.nl...
Michael Fesser wrote:
.oO(Jan Pieter Kunst)
But what happens in the $this->object = $obj; line in PHP 5? Is
$this->obj now a reference to a reference, as in ex. 1, assuming I
understood that piece of code correctly?

Nope. References are no pointers! They are just a kind of alias name.
Pointers to pointers exist, but a reference is always just that: an
alias name for a kind of object. There's no reference to a reference.

A-ha! Now things are getting clear. I was under the impression that
references are like pointers. OK, thanks very much for clearing that up.
Time to re-check the &'s in my PHP 4 code.


In general, avoid using references in PHP4. When you have

function A(&$obj) {
$this->object =& $obj;
}
}

$a = new A($the_object);

you are basically creating a reference to a variable holding a reference:

$this->object >>> $obj >>> $the_object

In order to get to the actually value, PHP has to dereference twice. Now
imagine you have a deep class structure, where all classes ultimately
inherit from A. The constructor of every class has to pass a reference to
the parent constructor. So you end up with references that goes like

$this->object >>> $obj >>> $obj >>>
$obj >>> $obj >>> $obj >>> $the_object

which makes your script dog slow and eat up memory, as you now have all
these function parameters that don't get freed after the function has ended
because they're being referred to.
Jul 17 '05 #5

P: n/a
On Tue, 21 Dec 2004 20:13:08 -0500, "Chung Leong" <ch***********@hotmail.com>
wrote:
"Jan Pieter Kunst" <de*****@cauce.org> wrote in message
news:41*********************@news.xs4all.nl...
Michael Fesser wrote:
> .oO(Jan Pieter Kunst)
>>But what happens in the $this->object = $obj; line in PHP 5? Is
>>$this->obj now a reference to a reference, as in ex. 1, assuming I
>>understood that piece of code correctly?
>
>
> Nope. References are no pointers! They are just a kind of alias name.
> Pointers to pointers exist, but a reference is always just that: an
> alias name for a kind of object. There's no reference to a reference.

A-ha! Now things are getting clear. I was under the impression that
references are like pointers. OK, thanks very much for clearing that up.
Time to re-check the &'s in my PHP 4 code.


In general, avoid using references in PHP4. When you have

function A(&$obj) {
$this->object =& $obj;
}
}

$a = new A($the_object);

you are basically creating a reference to a variable holding a reference:

$this->object >>> $obj >>> $the_object

In order to get to the actually value, PHP has to dereference twice. Now
imagine you have a deep class structure, where all classes ultimately
inherit from A. The constructor of every class has to pass a reference to
the parent constructor. So you end up with references that goes like

$this->object >>> $obj >>> $obj >>>
$obj >>> $obj >>> $obj >>> $the_object

which makes your script dog slow and eat up memory, as you now have all
these function parameters that don't get freed after the function has ended
because they're being referred to.


Do you have anything to demonstrate this (maybe need to dig down into the Zend
engine?), as it doesn't seem to match with the description in the manual?

"What References Are

References in PHP are a means to access the same variable content by different
names. They are not like C pointers, they are symbol table aliases."

If it's a symbol table alias, then you can't have a reference to a reference;
each reference is just one step away from the actual content?

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.