473,225 Members | 74 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,225 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 9455
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....
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.