Re: Calling methods of parent/ancestor classes in PHP5
| From: | Brad Fisher | Date: | Wed, 18 Feb 2004 17:02:33 +0000 |
| Subject: | Re: Calling methods of parent/ancestor classes in PHP5 | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-7958@lists.php.net to get a copy of this message | ||
Ferdinand Beyer wrote:
> On 17 Feb 2004 at 14:01, Brad Fisher wrote:
>
> > class D {
> > function bar() {
> > $c = new C;
> > // I'd like to call the A::foo method of C here...
> > // In PHP4, I could do something like:
>
> Why should one want to do that? Why should PHP allow this?
For one because PHP doesn't allow method overloading. If I have a method
A::foo($a), and I want to change the prototype in B to B::foo($x, $y), then
there is no way for me to call the original A::foo. I could use optional
params to emulate this, and I do as often as possible, so it typically isn't a
problem.
Also if I know A::foo does something I need/require, and C::foo does the
something a little different, and I also know A::foo is compatible with C, why
shouldn't I be able to do this? Or are you saying I should add a method to C
(like C::A_foo) which then calls A::foo internally? I'd hate to have to add
such wrappers in all cases, though I suppose it could work as well.
I guess I just like the flexibility. And of course it's a BC break, but it is
a major version...
> $c is not an A, it is a C. Of course C is a special version of A, so it is
> compatible to A. But it still is a C.
> There will be a reason why C has overridden the A implementation of
> foo(). It is possible that the original implementation is not compatible
> to C, so it could be a risk to call the old method.
That is potentially true, but if I intentionally coded the objects to ensure
compaitibility, then where's the problem? As mentioned above, it helps with
getting around the missing method overloading. In my opinion, a programming
language should enable the programmer to solve problems in ways that make
sense for the situation. One of the great strengths of PHP is that it allows
a lot of flexiblity. While it's true that such flexibility can be abused, it
can also be put to uses the original designers never anticipated.
> The feature you are asking for is not available in any OO language
> and it even conflicts with the OOP ideology!
Well, I guess I'd argue that Perl supports such calls (via
$obj->SomeClass::method() syntax)... Not that I'm saying that PHP should be
Perl, I much prefer coding in PHP!
Anyway, the "feature" was in PHP4, and people have used such constructs. Just
spend a little while looking through the comments for the Class/Object
functions in the PHP manual and you'll see examples that others have posted to
do this for PHP4.
> --
> Ferdinand Beyer
> <fb@fbeyer.com>
-Brad