ExpressionEngine插件中的未定义变量错误 [英] Undefined variable error in ExpressionEngine plugin

查看:103
本文介绍了ExpressionEngine插件中的未定义变量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基于外部库进行设备检测的插件.

I'm working on a plugin that does device detection based on an external library.

这是我到目前为止所拥有的:

This is what I have so far:

class Deetector {

// public $return_data;

/**
 * Constructor
 */
public function __construct()
{
    $this->EE =& get_instance();
    $this->EE->load->add_package_path(PATH_THIRD.'/deetector');
    $this->EE->load->library('detector');
    $this->return_data = "";
}

public function deetector()
{
    return $ua->ua;
}

public function user_agent()
{
    return $ua->ua;
}

// ----------------------------------------------------------------

/**
 * Plugin Usage
 */
public static function usage()
{
    ob_start();

    $buffer = ob_get_contents();
    ob_end_clean();
    return $buffer;
}
}

如果我致电{exp:deetector},则模板中没有任何输出.如果我致电{exp:deetector:user_agent},则会得到未定义变量:ua .

If I call {exp:deetector} I get no output in the template. If I call {exp:deetector:user_agent} I get Undefined variable: ua.

最终,我不打算为Detector库返回的每个变量设置不同的函数,而只是想让它现在输出一些东西.

Ultimately I don't plan on setting up different functions for each of the variables that the Detector library returns but am just trying to get it to output something at the moment.

我最初是作为扩展来执行此操作的,该扩展将Detector库的变量添加到全局变量数组中,并且运行良好.只是因为尝试将其作为插件进行操作,我才遇到问题.

I had originally started doing this as an extension which added the Detector library's variables to the global variables array and that was working fine; it's only since trying to do it as a plugin that I've run into problems.

推荐答案

您尚未将$this->ua设置为任何值.我认为这是您加载的检测器库的变量,因此您可能想要执行以下操作:

You haven't set $this->ua to anything. I assume it's a variable of the detector library you loaded, so you probably want to do something like this:

class Deetector {
    public function __construct()
    {
        $this->EE =& get_instance();

        // remove this line, it's probably not doing anything
        // $this->EE->load->add_package_path(PATH_THIRD.'/deetector');

        $this->EE->load->library('detector');

        // note you use $this->return_data instead of "return blah" in the constructor
        $this->return_data = $this->EE->detector->ua;
    }

    // remove this, you can't have both __construct() and deetector(), they mean the same thing
    // public function deetector()
    // {
    //     return $ua->ua;
    // }

    public function user_agent()
    {
        return $this->EE->detector->ua;
    }
}

更新:

我看了检测器docs ,并且不遵循常规的库约定(包含文件时定义了$ ua变量).因此,您应该忽略标准的EE加载功能,而直接包含文件:

I took a look at the Detector docs, and it doesn't follow normal library conventions (it defines the $ua variable when you include the file). For that reason you should ignore the standard EE load functions, and include the file directly:

class Deetector {
    public function __construct()
    {
        $this->EE =& get_instance();

        // manually include the detector library
        include(PATH_THIRD.'/deetector/libraries/detector.php');

        // save the local $ua variable so we can use it in other functions further down
        $this->ua = $ua;

        // output the user agent in our template
        $this->return_data = $this->ua->ua;
    }
}

这篇关于ExpressionEngine插件中的未定义变量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆