<?php
class foo
{
//this class is always etended, and has some other methods that do utility work
//and are never overrided
public function init()
{
//what do to here to call bar->doSomething or baz->doSomething
//depending on what class is actually instantiated?
}
function doSomething()
{
//intentionaly no functionality here
}
}
class bar extends foo
{
function doSomething()
{
echo "bar";
}
}
class baz extends foo
{
function doSomething()
{
echo "baz";
}
}
?>






public function init() {
$this->doSomething();
}
$obj = new bar();
$obj->doSomething(); // prints "bar"
$obj2 = new baz();
$obj->doSomething(); // prints "baz"
Вам просто нужно вызвать $ this-> doSomething (); в вашем методе init ().
Из-за полиморфизма правильный метод дочернего объекта будет вызываться во время выполнения в зависимости от класса дочернего объекта.