Re: An overloading question [message #174525 is a reply to message #174495] |
Wed, 15 June 2011 16:04 |
Michael Fesser
Messages: 215 Registered: September 2010
Karma:
|
Senior Member |
|
|
.oO(Jerry Stuckle)
> Unfortunately, PHP doesn't really implement OO constructs very well - in
> fact they to a pretty piss poor job of it. One of the problems is with
> virtual functions, as you found. A good OO language would work like you
> want, but PHP doesn't - the constructor for A will, in this case, only
> call a function in A.
What do you mean?
<?php
class A {
public function __construct() {
$this->y();
}
public function y() {
print __METHOD__."\n";
}
}
class B extends A {
public function y() {
print __METHOD__."\n";
}
}
$a = new A();
$b = new B();
?>
$ php -f constructor.php
A::y
B::y
Pretty much what I expect.
> In C++, Smalltalk, Java and most other OO
> languages, you could define y() as virtual and the constructor for A
> would call the function y() in B.
In PHP non-private methods are always virtual and in the above example
the constructor in A calls the method in B.
Micha
|
|
|