duanguochong0397 2010-09-10 14:06
浏览 31
已采纳

Smarty自定义插件使用基类

Here is my class that gets called on each page:

class ActionHandler {

    var $smarty = NULL;

    public function __construct() {

        if($this->smarty == NULL){

            $this->smarty = new Smarty();

            $this->smarty->template_dir = TEMPLATE_DIR;

            $this->smarty->compile_dir = COMPILE_DIR;

        }

    }

    public function do_something($page_id) {
        return $page_id + 1;
    }
}   

Now I have a custom plugin for smarty that I want to use in my template:

function smarty_function_something($params, &$smarty) {
    return ActionHandler::do_something($params['page_id']);
}

However I get Fatal error: Using $this when not in object context.

I see why but don't know how to get around this. Any ideas?

  • 写回答

2条回答 默认 最新

  • dqkelut8423 2010-09-10 14:13
    关注

    Try making the do_something a static member of ActionHandler

    class ActionHandler {
    
        public static $smarty = NULL;
        public function __construct()
        {
            if($this->smarty == NULL)
           {
                $this->smarty = new Smarty();
                $this->smarty->template_dir = TEMPLATE_DIR;
                $this->smarty->compile_dir = COMPILE_DIR;
    
           }
       }
    
        public static function do_something($page_id)
        {
            return $page_id + 1;
        }
    }
    

    As your trying to access a non static method i *think that the __construct gets executed before the method is available, but as you have not created an instance of the object, the keyword $this does not exists.

    you have to create specific static methods. if your going MyObject::SomeMethod($param)

    you should also take a look at Object Auto Loading and Auto Initializing objects via static methods.

    also you don't need to specifically define the value to public static $smarty = NULL; as Null is a default value of any new variable, just do

    public static $smarty;
    

    going a little more indepth with your problem you should add a singleton method like so..

    class ActionHandler
    {
        public static $smarty;
        public static $singleton;
        public function __construct()
        {
            if($this->smarty == NULL)
           {
                $this->smarty = new Smarty();
                $this->smarty->template_dir = TEMPLATE_DIR;
                $this->smarty->compile_dir = COMPILE_DIR;
    
           }
       }
        public static GetSingleton()
        {
            if(self::$singleton == null)
            {
                 self::$singleton = new ActionHandler();
            }
            return self::$singleton;
        }
    
        public static function do_something($page_id)
        {
            $_this = self::GetSingleton();
    
    
            return $page_id + 1;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)