in PHP 5, $obj1 = $obj2 not the same as $obj1 =& $obj2
in PHP 5, $obj1 = $obj2 not the same as $obj1 =& $obj2
am 27.09.2007 17:11:31 von Summercoolness
seems like in PHP 5, $obj1 = $obj2 is not the same as $obj1 =& $obj2
although the manual says...
http://www.php.net/manual/en/language.operators.assignment.p hp
> 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 ---------------------
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 ---------------------
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!
)
Re: in PHP 5, $obj1 = $obj2 not the same as $obj1 =& $obj2
am 27.09.2007 17:43:20 von gosha bine
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/language.operators.assignment.p hp
>
>> 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
Re: in PHP 5, $obj1 = $obj2 not the same as $obj1 =& $obj2
am 27.09.2007 18:12:59 von Summercoolness
On Sep 27, 8:43 am, gosha bine 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.
>
> --
> 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).
Re: in PHP 5, $obj1 = $obj2 not the same as $obj1 =& $obj2
am 27.09.2007 18:30:45 von zeldorblat
On Sep 27, 12:12 pm, Summercool wrote:
>
> 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.
Re: in PHP 5, $obj1 = $obj2 not the same as $obj1 =& $obj2
am 27.09.2007 18:47:46 von gosha bine
Summercool wrote:
> On Sep 27, 8:43 am, gosha bine 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.
>>
>> --
>> 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
Re: in PHP 5, $obj1 = $obj2 not the same as $obj1 =& $obj2
am 27.09.2007 22:49:22 von Summercoolness
On Sep 27, 8:43 am, gosha bine wrote:
> 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...
Re: in PHP 5, $obj1 = $obj2 not the same as $obj1 =& $obj2
am 27.09.2007 22:50:09 von Summercoolness
On Sep 27, 8:43 am, gosha bine wrote:
> 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?
Re: in PHP 5, $obj1 = $obj2 not the same as $obj1 =& $obj2
am 27.09.2007 23:00:58 von gosha bine
Summercool wrote:
> On Sep 27, 8:43 am, gosha bine wrote:
>> 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
Re: in PHP 5, $obj1 = $obj2 not the same as $obj1 =& $obj2
am 27.09.2007 23:02:30 von gosha bine
Summercool wrote:
> On Sep 27, 8:43 am, gosha bine wrote:
>
>> 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
Re: in PHP 5, $obj1 = $obj2 not the same as $obj1 =& $obj2
am 28.09.2007 05:47:41 von Jerry Stuckle
Summercool wrote:
> On Sep 27, 8:43 am, gosha bine 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.
>>
>> --
>> 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.
jstucklex@attglobal.net
==================
Re: in PHP 5, $obj1 = $obj2 not the same as $obj1 =& $obj2
am 28.09.2007 05:49:54 von Jerry Stuckle
gosha bine wrote:
> Summercool wrote:
>> On Sep 27, 8:43 am, gosha bine 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.
>>>
>>> --
>>> 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.
jstucklex@attglobal.net
==================