smarty3--registerPlugin()函数报错问题

smarty版本:smarty3.1.30

registerPlugin错误信息:

Notice: Trying to get property of non-object in E:\Joomla\system\libs\sysplugins\smarty_internal_templatebase.php on line 245

Fatal error: Call to a member function registerPlugin() on null in E:\Joomla\system\libs\sysplugins\smarty_internal_templatebase.php on line 245

 

代码示例:

system.smarty.inc.php  //smarty模板配置类文件

<?php
require("libs/Smarty.class.php");  //引入smarty类库
class SmartyProject extends Smarty{
    function __construct(){  //配置信息
        $this->template_dir="./system/templates/";
        $this->compile_dir="./system/templates_c/";
        $this->config_dir="./system/configs/";
        $this->cache_dir="./system/catch/";
    }
}
?>

 

system.inc.php  //类的实例化文件

<?php
require("system.smarty.inc.php");
require("system.class.inc.php");
$usefun=new UseFun();
$smarty=new SmartyProject;


function unhtml($params){
    extract($params);
    $text=$content;
    global $usefun;
    return $usefun->UnHtml($text);
}
$smarty->registerPlugin('function','unhtml','unhtml');  //注册模板函数
?>

 

此时会提示错误信息:

Notice: Trying to get property of non-object in E:\Joomla\system\libs\sysplugins\smarty_internal_templatebase.php on line 245

Fatal error: Call to a member function registerPlugin() on null in E:\Joomla\system\libs\sysplugins\smarty_internal_templatebase.php on line 245

 

解决办法:在smarty配置类文件中,引入父类的构造方法。即将system.smarty.inc.php文件改为:

require("libs/Smarty.class.php");
class SmartyProject extends Smarty{
    function __construct(){
        parent::__construct();  //引入父类的构造方法
        $this->template_dir="./system/templates/";
        $this->compile_dir="./system/templates_c/";
        $this->config_dir="./system/configs/";
        $this->cache_dir="./system/catch/";
    }
}

 

此时函数注册成功!

posted on 2016-12-22 11:31  平淡日子里的刺  阅读(669)  评论(0编辑  收藏  举报

导航