ExpressionEngine插件中未定义的变量错误

时间:2012-10-26 23:04:36

标签: expressionengine

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

这是我到目前为止所做的:

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

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

我最初开始将此作为扩展,将Detector库的变量添加到全局变量数组中,并且工作正常;它只是因为尝试将其作为插件解决了我遇到的问题。

1 个答案:

答案 0 :(得分:2)

您尚未将$this->ua设置为任何内容。我假设它是你加载的检测器库的一个变量,所以你可能想要做这样的事情:

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;
    }
}

<强>更新

我查看了Detector docs,它没有遵循正常的库约定(它在您包含文件时定义了$ ua变量)。因此,您应该忽略标准EE加载函数,并直接包含文件:

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;
    }
}