[nycphp-talk] static methods in derived classes
David Sklar
sklar at sklar.com
Thu Sep 23 09:58:49 EDT 2004
If you call, in a subclass, a static method defined in the parent class,
"self" inside the method refers to the parent class. This is causing me
problems, e.g. here:
class Meal {
public static function eat() {
$food = self::prepare();
self::chew($food);
}
}
class Lunch extends Meal {
protected static function prepare() { return 'Sandwich'; }
protected static function chew($food) { print $food; }
}
Doing:
Lunch::eat();
Produces:
Fatal error: Call to undefined method Meal::prepare()
Any thoughts on how to refer to the child methods in the parent class?
Or why this is stinky OO style and what I should do instead?
Note that with non-static methods, $this works (as expected) just fine.
The code:
class Snack {
public function eat() {
$food = $this->prepare();
$this->chew($food);
}
}
class Tea extends Snack {
protected function prepare() { return 'Cucumber Sandwich'; }
protected function chew($food) { print $food; }
}
$t = new Tea;
$t->eat();
Prints:
Cucumber Sandwich
Thanks,
David
More information about the talk
mailing list