You are here: Re: PHP5 and class inheritance question « PHP Programming Language « IT news, forums, messages
Re: PHP5 and class inheritance question

Posted by Michael Fesser on 12/19/07 20:21

..oO(Steve)

>"Michael Fesser" <netizen@gmx.de> wrote in message
>news:0hlim3hgb5p7ifm2o13pjm6q4l0q5jhiim@4ax.com...
>>
>> 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.

A handle is not 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.

Exactly. A pointer contains a memory address, a handle doesn't.

>in php, a reference (or byref) behaves *IDENTICALLY* to a c/c++ pointer.

Nope. References in PHP behave like references in C++, they're just
alias names for the same data structure. That's a big difference to
pointers, which don't exist in PHP. For example the memory address a
pointer points to could be another pointer as well. Such things are not
possible with references, neither in C++ nor in PHP. If you assign a
reference to a reference, it'll just become an additional alias.

>> 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.

Correct, but it probably makes it easier and more efficient.

>> 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.

The combination handle->object is kind of a special reference (I don't
like to call it like that to avoid too much confusion). Together with
the "normal" references it might even become reference->handle->object.

>> 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.

That entry _is_ the handle. The associated object can be stored
elsewhere (I don't know how the ZE handles this internally, doesn't
matter anyway).

>$foo becomes an alias for that handle.

An alias would be a reference, which is not the case here.

>$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.

Let's look at it this way:

$foo = 5;

$a = $foo;
$b = &$foo;

Same thing. Let the 5 be the internal number (the handle) of any object.
The first assignment copies the symbol table entry of $foo into $a.
Both $foo and $a now contain the same value, but of course in different
symbol table positions - both values can be changed without affecting
the other. The second assignment doesn't make a copy, but instead makes
$b an alias/a reference to the symbol table entry of $foo:

$foo ----.
+---> 5
$b ----´

$a --------> 5

This is a bit simplified and not exactly how it's done internally, but
it's how it behaves from the programmer's POV.

>$a is NOT a copy of the $foo's object handle - that would be an *alias*...a
>*reference*.

$b is the reference, $a is the copy. Back to our object handle, the
situation would look like this:

$foo ----.
+---> 5 ----.
$b ----´ +---> object #5
|
$a --------> 5 ----´

So if you now change $b to something else, it will also change $foo
because of the reference. But if you change $a, it will leave $b and
$foo untouched:

$foo ----.
+---> 5 --------> object #5
$b ----´

$a --------> 42

>> 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?

Mentioned in the first paragraph:

| If you're among the crowd who have migrated an OOP based application
| from PHP4 to PHP5, then I'm sure you've heard the expression "Objects
| are copied by reference by default in PHP5". Whoever told you that,
| was lying.

Micha

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация