473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

pass objects by reference (PHP4/5)

(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
5 9470
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
.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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: lawrence | last post by:
The following class method is being rejected by the PHP parser. If I change the method paramaters and allow the objects to be passed as copies, then the parser has no problem. Or, if I pass by...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
7
by: John | last post by:
Hi I open a word document from my vb.net app. Now I want to pass reference to one of the classes in my vb.net app to word so it can access procedures in the vb.net class. How can I pass the...
3
by: tinman | last post by:
Hi.... Assume Function A in an application calls Function GetSomeData in another assembly..... which then is the prefered method to return the SqlDatareader object back to Function A (and why...
5
by: David++ | last post by:
Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into...
5
by: josh | last post by:
Hi, how really work behind the scene the pass-by-reference in C++? I know that in C++ like the C the objects are passed by-values and if we want we can simulate the pass-by-reference using the...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
13
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.