在框架问题2的答案中,主题是基本主题qa_html_theme_base
的扩展。在这个例子中,我扩展了输出html的html函数。
class qa_html_theme extends qa_html_theme_base
{
function html(){
//Theme goes here
}
}
我希望能够快速打开和关闭我的主题以进行测试。有可能有条件地扩展一个类吗?我试过
class qa_html_theme extends qa_html_theme_base
{
if($debug){
function html(){}
}
}
但它没有起作用。
我不确定这是否可能,类声明中的这种语法是不正确的。如果是的话,我不确定我会推荐它
但是,如果您的函数覆盖了一个扩展类函数,您可以执行以下操作:
class qa_html_theme extends qa_html_theme_base
{
function html(){
global $debug; // added to maintain a correct syntax, but you could as well use $this->debug below, if the value comes from a class property.
if( $debug ){
// your debug code here
}
else {
parent::html();
}
}
}
我能想到的唯一方法就是有条件地包含类文件,以实现您的建议(这充其量是笨拙的)。因此,创建两个类文件。我们将调用第一个theme_html.php
,它包括您的html()
函数。第二个是theme_no_html.php
,它没有html()
函数。这很麻烦,因为您需要维护两个文件。
然后我们做
if($debug) {
include('theme_html.php');
} else {
include('theme_no_html.php');
}
$class = new qa_html_theme();
如果我的想法是正确的,
class qa_html_theme extends qa_html_theme_base
{
protected $debug = 1; // or const DEBUG = 1;
/**
* Constructor
*/
public function __construct()
{
if($this->debug){
$this->html();
}else{
// do anything you want
}
}
protected function html(){
//Theme goes here
}
}