|
Posted by Steve on 12/19/07 19:04
"Michael Fesser" <netizen@gmx.de> wrote in message
news:0hlim3hgb5p7ifm2o13pjm6q4l0q5jhiim@4ax.com...
> .oO(Logos)
>
>>On Dec 13, 3:16 pm, Michael Fesser <neti...@gmx.de> wrote:
>>
>>> At least when working with objects. But nevertheless
>>>
>>> $foo = new Test();
>>> $a = $foo;
>>> $b = &$foo;
>>>
>>> are still different things, even in PHP 5. In some particular situations
>>> this might become an issue.
>>
>>Oooo...errr...ummm...could someone explain how exactly those are
>>different when using PHP5, then, please? If everything is done by
>>reference for objects, then to me $a and $b both look like pointers to
>>an object.
>
> Don't confuse pointers with references, they are entirely different
> things. PHP doesn't know pointers.
>
> And correctly spoken objects in PHP 5 are _not_ passed by reference (at
> least it's not what PHP calls a reference), even if it's still mentioned
> that way on many websites. But it's wrong.
>
> Internally objects are represented by a handle (a simple number), which
> is what is moved around when you assign objects to variables, copy them
> or pass them to a function. You're never working directly with the
> object itself, but with its handle. Of course usually you won't notice
> that, because it's handled transparently by PHP.
michael, for people who come from a c/c++ background, what you've described
is *exactly* a pointer. the only difference in php is that rather than the
handle pointing to a memory address where information is stored, this php
handle points to a symbol table entry where information is stored.
in php, a reference (or byref) behaves *IDENTICALLY* to a c/c++ pointer.
there are somethings that you cannot do with this reference in php that you
could in other languages, however, the nature of the beast is the same. i
know that a reference in php is really just an alias of the symbol table
entry, but really that just seems a matter of symantics to me. i don't care
where things are stored at such a low level when i'm writing in a scripting
language. i care about behaviors.
> New Object Model
> http://www.php.net/manual/en/migration5.oop.php
>
> | [Objects in PHP 4] The drawback of this method was that semantically
> | the whole object was copied when a variable was assigned, or passed as
> | a parameter to a method. In the new approach, objects are referenced
> | by handle, and not by value (one can think of a handle as an object's
> | identifier).
to be accurate, the handle needn't be a number.
> Of course in addition to these object handles there are still the normal
> PHP references, which you can use as well.
please explain, as there are no 'special' references in php. you seem to be
comparing a reference directly with a thing that makes references work.
> So in the example code above
> $a contains a copy of the object handle which was created beforehand,
> while $b is a reference to that handle. That's a difference.
no, this is wholly wrong.
new object() creates an entry in the symbol table. that entry has a handle.
$foo becomes an alias for that handle. $a gets a new handle in the symbol
table whereby the entry data is copied into it from $foo. $b is an alias of
$foo.
$a is NOT a copy of the $foo's object handle - that would be an *alias*...a
*reference*. $foo's symbol data is copied into a new entry and $a gets the
handle for that entry. it's an important distinction.
> Here's a background article regarding this issue (and some more):
>
> You're being lied to.
> http://blog.libssh2.org/index.php?/archives/51-Youre-being-lied-to..html
by whom, micha?
Navigation:
[Reply to this message]
|