473,473 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

in PHP 5, $obj1 = $obj2 not the same as $obj1 =& $obj2

seems like in PHP 5, $obj1 = $obj2 is not the same as $obj1 =& $obj2

although the manual says...

http://www.php.net/manual/en/languag...assignment.php
Since PHP 4, assignment by reference has been supported,
using the $var = &$othervar
As of PHP 5, objects are assigned by reference
unless explicitly told otherwise with the new clone
keyword.
However, $obj1 = $obj2
and $obj1 = &$obj2 are two different things...

the first one is "assignment by reference"
the second one is "assignment by synonym"

----------------- assign1.php ---------------------

<?php

class Foo {
var $name;

function Foo($i) {
$this->name = "I'm $i!\n";
}
}

$a = new Foo("ha");
$b = $a;
$b = new Foo("hee");

echo "This is php ", phpversion(), "\n\n";
print_r($a);
print_r($b);

?>

----------------- output

This is php 5.2.4

Foo Object
(
[name] =I'm ha!

)
Foo Object
(
[name] =I'm hee!

)
----------------- assign2.php ---------------------

<?php

class Foo {
var $name;

function Foo($i) {
$this->name = "I'm $i!\n";
}
}

$a = new Foo("ha");
$b = &$a;
$b = new Foo("hee");

echo "This is php ", phpversion(), "\n\n";
print_r($a);
print_r($b);

?>

----------------- output

This is php 5.2.4

Foo Object
(
[name] =I'm hee!

)
Foo Object
(
[name] =I'm hee!

)

Sep 27 '07 #1
10 1892
Summercool wrote:
seems like in PHP 5, $obj1 = $obj2 is not the same as $obj1 =& $obj2

although the manual says...

http://www.php.net/manual/en/languag...assignment.php
>Since PHP 4, assignment by reference has been supported,
using the $var = &$othervar
>As of PHP 5, objects are assigned by reference
unless explicitly told otherwise with the new clone
keyword.
The manual is incorrect at this point. Object variables in php5 are
assigned by value, just like in php4, but this very value is different
in php5 - it's a pointer to the object, not the object itself. But this
is essentially another story and has nothing to do with references.
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Sep 27 '07 #2
On Sep 27, 8:43 am, gosha bine <stereof...@gmail.comwrote:
The manual is incorrect at this point. Object variables in php5 are
assigned by value, just like in php4, but this very value is different
in php5 - it's a pointer to the object, not the object itself. But this
is essentially another story and has nothing to do with references.

--
gosha bine
i think pointer and reference are the same thing, like in Java and
Ruby.
In PHP, what the manual calls "reference" is actually not a reference
in other languages... it is like an alias or (a nickname).
Sep 27 '07 #3
On Sep 27, 12:12 pm, Summercool <Summercooln...@gmail.comwrote:
>
i think pointer and reference are the same thing, like in Java and
Ruby.
In PHP, what the manual calls "reference" is actually not a reference
in other languages... it is like an alias or (a nickname).
Correct -- it is a "symbol table alias."
>From the manual:
"References in PHP are a means to access the same variable content by
different names. They are not like C pointers; instead, they are
symbol table aliases. Note that in PHP, variable name and variable
content are different, so the same content can have different names."

Personally I think that's much simpler than the reference model in
other languages.

Also be aware of the distinction between "passing by reference" (as
in, passing to a function) and assigning references.

Sep 27 '07 #4
Summercool wrote:
On Sep 27, 8:43 am, gosha bine <stereof...@gmail.comwrote:
>The manual is incorrect at this point. Object variables in php5 are
assigned by value, just like in php4, but this very value is different
in php5 - it's a pointer to the object, not the object itself. But this
is essentially another story and has nothing to do with references.

--
gosha bine

i think pointer and reference are the same thing, like in Java and
Ruby.
The term "reference" can be used in the generic sense, describing a
value that is used to access another value. In this sense, a pointer is
a (kind of) reference. However, in c++ and php the word "reference" has
another meaning, and describes some specific construct, that has nothing
to do with pointers. Object variables in php5 are pointers and not
references (in c++/php sense).
In PHP, what the manual calls "reference" is actually not a reference
in other languages... it is like an alias or (a nickname).
Good that you understand this. It means your original problem is cleared
up. ;)

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Sep 27 '07 #5
On Sep 27, 8:43 am, gosha bine <stereof...@gmail.comwrote:
Summercool wrote:
The manual is incorrect at this point. Object variables in php5 are
assigned by value, just like in php4, but this very value is different
in php5 - it's a pointer to the object, not the object itself. But this
is essentially another story and has nothing to do with references.
I just wonder if the manual is incorrect equating PHP4's =& to
PHP5's obj1 = obj2, then how come it is still there on the website
for so long... and for the whole PHP user audience...
Sep 27 '07 #6
On Sep 27, 8:43 am, gosha bine <stereof...@gmail.comwrote:
gosha bine

extended php parser ~http://code.google.com/p/pihipi
blok ~http://www.tagarga.com/blok
do you work for Google? is the PiHiPi pretty good?
Sep 27 '07 #7
Summercool wrote:
On Sep 27, 8:43 am, gosha bine <stereof...@gmail.comwrote:
>Summercool wrote:
>The manual is incorrect at this point. Object variables in php5 are
assigned by value, just like in php4, but this very value is different
in php5 - it's a pointer to the object, not the object itself. But this
is essentially another story and has nothing to do with references.

I just wonder if the manual is incorrect equating PHP4's =& to
PHP5's obj1 = obj2, then how come it is still there on the website
for so long... and for the whole PHP user audience...

I'm wondering that too. I consider this wording quite harmful because
I've seen many people confused by the idea that php5 objects have
anything to do with references.
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Sep 27 '07 #8
Summercool wrote:
On Sep 27, 8:43 am, gosha bine <stereof...@gmail.comwrote:
>gosha bine

extended php parser ~http://code.google.com/p/pihipi
blok ~http://www.tagarga.com/blok

do you work for Google? is the PiHiPi pretty good?

no, yes
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Sep 27 '07 #9
Summercool wrote:
On Sep 27, 8:43 am, gosha bine <stereof...@gmail.comwrote:
>The manual is incorrect at this point. Object variables in php5 are
assigned by value, just like in php4, but this very value is different
in php5 - it's a pointer to the object, not the object itself. But this
is essentially another story and has nothing to do with references.

--
gosha bine

i think pointer and reference are the same thing, like in Java and
Ruby.
In PHP, what the manual calls "reference" is actually not a reference
in other languages... it is like an alias or (a nickname).

No, pointer and reference are NOT the same thing. It's a common
misconception which screws a lot of people up when the difference is
important.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 28 '07 #10
gosha bine wrote:
Summercool wrote:
>On Sep 27, 8:43 am, gosha bine <stereof...@gmail.comwrote:
>>The manual is incorrect at this point. Object variables in php5 are
assigned by value, just like in php4, but this very value is different
in php5 - it's a pointer to the object, not the object itself. But this
is essentially another story and has nothing to do with references.

--
gosha bine

i think pointer and reference are the same thing, like in Java and
Ruby.
Not at all. I don't know about Ruby, but Java doesn't have pointers.
The term "reference" can be used in the generic sense, describing a
value that is used to access another value. In this sense, a pointer is
a (kind of) reference. However, in c++ and php the word "reference" has
another meaning, and describes some specific construct, that has nothing
to do with pointers. Object variables in php5 are pointers and not
references (in c++/php sense).
And that thinking really screws people up when the difference is important.

It's like an integer 1 and floating point 1.0. In many cases it's fine.
But not always.
>In PHP, what the manual calls "reference" is actually not a reference
in other languages... it is like an alias or (a nickname).

Good that you understand this. It means your original problem is cleared
up. ;)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 28 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: m|sf|t | last post by:
All, I am stuck processing an XML file. The problem I am having, is I have the same TAG name - Url - at the same depth, so the value from the last Url read in is what get saved. I have tried every...
1
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
0
by: Thomas Scheffler | last post by:
Hi, I runned in trouble using XALAN for XSL-Transformation. The following snipplet show what I mean: <a href="http://blah.com/?test=test&amp;test2=test2">Test1&amp;</a> <a...
1
by: John Cho | last post by:
if i do a friend Testclass operator +(Test class &obj2); it is not correct because i need two objects?
4
by: MLH | last post by:
A programmer developed an AMP (Apache/MySQL/PHP) application for me. When he was done, he sent me the PHP files and the MySQL dump file. Now, when I connect to the application on my LAN using...
4
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
3
by: vijay | last post by:
Hi, I am using struts frame work and in one of the jsp pages I have a bunch of IFRAMES like this.(This is in logic iterate and the recordId is incremented with that iteration)...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
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
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
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...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.