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

Posted by Steve on 12/19/07 21:47

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

a handle in php lets php lookup an entry in the symbol table.
a c-style pointer is value in a memory address that specifies where another
memory adress is located wherein the data resides for whatever the pointer
is trying to reference.

i was making no claims that a handle is or is not a pointer. i was pointing
out that they behave similarly...in fact, functionally identically in php.

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

again, this is irrelevant to behavior. the end result is the same whether
the data is in memory or in a symbolic table. i'm talking about behavior.
i'd be interested in that level of detail if i weren't using a *scripting*
language. for most however, it is enough to describe the behavior of php
references as pointers...especially for those whom have a background in
working with pointers. and for those who don't, 'pointer' is a great word to
describe the lookup nature of php references. not technically accurate,
however it is very effective and bypasses a lot of technical stuff they
really won't care less about.

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

did you miss a 'not' or 'don't' somewhere in there? you just agreed with me
yet preficed it with 'nope'. as for pointer v. reference in a specific
language? the differences or to minute to haggle over when it comes to
either being used to the *behavior* of references in php.

> That's a big difference to
> pointers, which don't exist in PHP.

behavior

> For example the memory address a
> pointer points to could be another pointer as well.

minutia. if i use a pointer that points to an nth degree of others, i'm
still manipulating a source object stored somewhere. whatever that looks
like behind the php scenes, i don't care...the end result is identicle.

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

but all still end up, regardless of mechanics, working on the same source
object.

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

not sure that it does, fwir, the lookup on the symbol table is essentially
like using a key on an array, and there's no speed difference there whether
your key is numeric or alpha or both. were it stored in a db, i'm sure that
would be true though.

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

right, which is why i don't trifle with the technical differences we are
both aware of when i say pointer, reference, or alias. in the end, and no
matter how many -> are inbetween the variable and the object, the object is
always returned. i do get technical when using a technical language, just
not in php...esp. on usenet. i'm considering the audience.

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

you mean the 'z entry' ... aka, zval, or the zend php engine? i'm a bit
fuzzy at present. the zval is a descriptive entry in the symbols table. one
of those descriptions is the 'handle'...they key of the data element in the
array, if you get the analogy. the handle is not itself, the entry but a set
of descriptors that constitute the full entry. does that address what you
are talking about here?

>>$foo becomes an alias for that handle.
>
> An alias would be a reference, which is not the case here.

alias == reference in php. the key word 'new' makes an entry which will have
a handle. $foo gets the reference/alias to that handle. the handle is
created whether or not 'new object()' is on a line by itself or if it is a
rhs assignment.

consider a static class. whether or not you put a getInstance() interface on
it or not, all of the proper descriptors for the class will be there when
the first use of one of its interfaces is accessed.

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

again, in the order of operations, 5 would have been the result of the rhs
creation of the object using the keyword 'new'. $foo is now an alias for
that object. but, proceed.

> The first assignment copies the symbol table entry of $foo into $a.

no, it changes the descriptors of the entry such that ref_count is two and
is_ref remains 0. upon the first write operation done on $a, the $foo entry
is copied into an entry just for $a and the values being changed in $a will
be seen in that entry.

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

yes and no. before a write operation, all entry details are kept in the $foo
entry. it saves processing hits and perserves speed that way. when a value
changes between either, you will have two independent entries created for
$foo and for $a...but at this point, i think i'm getting too
technical...which is my point in not worrying so much about using pointer,
alias, and reference interchangably.

> The second assignment doesn't make a copy, but instead makes
> $b an alias/a reference to the symbol table entry of $foo:

kind of, however from what i recall, the entry table looks almost identical
before a write operation. the difference in the byref entry is that is_ref
will be true...$b will have simply increased the ref_count - my crippled
memory notwithstanding of course.

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

it's interesting just to focus on what happens to the rhs object variable in
each assignment. $foo will have it's object data changed by using interfaces
on $b. while its not memory, it sure seems that $b points to, or references,
or is an alias of $foo. see where i'm going in laymens terms? in laymens
terms, $a seems to be an independent copy of, or, clone of $foo. changing $a
has no effect on $foo.

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

if $a is to be a copy of $foo, it will not have the same handle.

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

right, we have no confusion there...much less anywhere else i think. i'm
just a bit more comfortable using descriptive terms in describing how =& and
function arguments are performed in php. of course that changes with
context...but, php is a scripting language. the audience is too, more
relaxed.

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

oh, so the 'whom' is 'whoever'...i'm grinning right now. :)

unless i read the article, which most won't, it seems as though you are
talking about someone who posted a comment in the thread. i don't see anyone
lying here. i'm glad you posted the paragraph, as i would have assumed you
were talking about me. :)

cheers

 

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

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация