<pre> <?php class S { protected $_data = array(); public function __construct($separator) { $name = get_class($this); for ($i = 1; $i < 3; $i++) { for ($j = 1; $j < 3; $j++) { $this->_data[$i][$j] = sprintf('Property %d%s%d of %s', $i, $separator, $j, $name); } } } public function get($id, $pid) { return isset($this->_data[$id][$pid]) ? $this->_data[$id][$pid] : null; } } class S1 extends S { } class S2 extends S { } abstract class A { abstract protected function i(); public static function getById($id, $property_id) { if ($id <= 0 || $property_id <= 0) { throw new Exception('Неверные данные'); } return static::i()->get($id, $property_id); } } class B extends A { private static $instance; protected function i() { if (!self::$instance) { $len = rand(1, 20); self::$instance = new S1(str_repeat('-', $len)); } return self::$instance; } } class C extends A { protected function i() { $len = rand(1, 20); return new S2(str_repeat('*', $len)); } } for ($i = 0; $i < 5; $i++) { ?> <div style="border:1px #ccc dashed;"><?php var_dump(B::getById(rand(1,2), rand(1,2))); var_dump(C::getById(rand(1,2), rand(1,2)));?> </div> <?php }
If there will be "return self::i()->get($id, $property_id);", you will get
Fatal error: Cannot call abstract method A::i() in /var/web/tests/lsb_2.php on line 37
Call Stack:
0.0003 350596 1. {main}() /var/web/tests/lsb_2.php:0
0.0003 350728 2. A::getById() /var/web/tests/lsb_2.php:65