我尝试做类树,其中树中的每个类检查模板的自己的目录并使用它,但是当我在继承的类中调用函数,然后调用父类。我该怎么做 ?PHP:继承问题
我的例子下面输出在代码:
d
Ç
B/1.phtml
但我需要d/1.phtml
<?php
class A {
private $templates_dir = 'a';
}
class B extends A {
private $templates_dir = 'b';
public function templates_dir()
{
return $this->templates_dir;
}
public function check_template($tpl)
{
$dir = $this->templates_dir();
$file = $dir. '/'. $tpl;
echo (get_class($this)). "\r\n";
echo (get_parent_class($this)). "\r\n";
echo $file . "\r\n";
// idea - if (!file_exists($file)) return parent::check_template($file);
// method call each class while template will be found
// how do it?
}
}
class C extends B {
private $templates_dir = 'c';
}
class D extends C {
private $templates_dir = 'd';
}
$obj = new D();
$obj->check_template('1.phtml');
与所有那些你在痛苦的世界进入的子类。 – Federkun