473,585 Members | 2,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9481
On Tue, 21 Dec 2004 06:23:34 +0100, Jan Pieter Kunst <jp*@akamail.co m> 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.andyhsoftwa re.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($ob j) {
$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******** *************@n ews.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.andyhsoftwa re.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
2840
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 reference but set no default value, then the parser has no problems. I've resovled this for now by taking out the "false" default values. But how shall...
110
9866
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 must be an object instead of
7
2383
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 class reference to word from a vb.net app? Thanks Regards
3
1748
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 ?). Does the prefered option apply to all reference types ? Option 1 *******
5
7809
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 a WebService is that essentially we are passing an address of an object which resides on the 'local machine' i.e. a local machine object address....
5
2309
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 pointers... Bye
10
13637
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 function is also modified. Sometimes I wish to work with "copies", in that when I pass in an integer variable into a function, I want the...
13
3012
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 bother in C# with pass-by-reference using the "ref" keyword or "out" for objects in a method parameter list, when, after all, it appears in C# that for...
12
3003
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. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables....
0
7908
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8199
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8336
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2343
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.