-->

在ExpressionEngine插件未定义变量错误(Undefined variable erro

2019-10-17 08:45发布

我的工作,做基于外部库设备检测插件。

这是我到目前为止有:

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。

最后,我不打算为每个探测器库的回报,但我只是试图得到它输出的东西,此刻的变量设置不同的功能。

本来我开始做这个作为添加探测器库的变量,全局变量数组的延伸,这是做工精细; 这只是因为试图做到这一点的,我已经遇到问题的插件。

Answer 1:

您还没有设置$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;
    }
}

更新:

我接过一看探测器文档 ,并且它不遵循正常库约定(它定义了$ 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;
    }
}


文章来源: Undefined variable error in ExpressionEngine plugin