why 'parent::' and not '$super->'?
[email protected] ("Dean Hall") Fri, 29 Jun 2001 21:24:28 -0500
| Newsgroups | php.lang |
|---|---|
| Message-ID | <[email protected]> |
(Note: I don't develop for the Zend engine, and I haven't developed any PHP
extensions (although I plan to); I'm not familiar with the PHP source, so if
you want to ignore my opinion b/c of this, please feel free not to read
further.)
The documentation on the scope-resolution operator ('::'), the 'parent'
keyword, and class inheritance is a bit more oriented towards newbies (to
PHP and to OOP), so I thought I'd ask here.
It seems that the scope-resolution operator (I'm calling it this 'cause
that's what it's called in C++, and the manual just calls it '::'), since
there are no namespaces to resolve, should only be used to call class
methods (i.e., static methods) -- that is, you should never be able to do:
'$objectInstance::method();'.
But it seems that this is exactly what 'parent::method()' does. As far as
the manual says, the 'parent' keyword is just the name of the superclass. If
this is the case, calling 'parent::method()' and having a reference to
'$this' in 'method()' should be impossible. But this is exactly what
happens. I'm guessing that language parser determines whether the method in
which 'parent::method()' is in was called as an instance or static method
and then calls 'method()' appropriately (read, magically). As a language
feature, this is poor, IMHO.
Methods should be called statically with the scope-resolution operator
('::'), and instance methods should be called with the arrow ('->'
dereferencing?) operator. (Yes, I know that any method in a PHP class can
potentially be either an instance or a static method.) Wouldn't a '$super'
reference in any instance method of a class be appropriate? It sure would
make things less foggy than mud for those of us syntax-Nazis out there. :-)
This way, all of us OOP-lovers could call '$super->doThis()' to our heart's
contents.
'$super' is appropriate in PHP as PHP does not support multiple inheritance
(i.e., a class has at most one superclass), and it already supports the
Java-(and C++-)style '$this' reference. '$super->method()' could resolve at
runtime and simply fail as a parse/fatal error if the class has no
superclass -- or it could be caught at interpret-time with a bit of work.
Just my $0.02 USD.
Dean Hall.