1

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

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

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.

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.

Tyssen
  • 1,569
  • 16
  • 35

1 Answers1

2

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

UPDATE:

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;
    }
}
Adrian Macneil
  • 13,017
  • 5
  • 57
  • 70
  • I've done that and get 'Undefined property: Detector::$ua' for `$this->return_data = $this->EE->detector->ua;` – Tyssen Oct 26 '12 at 23:20
  • What is the `ua` property? Does it even exist? Undefined property simply means you're trying to access a variable which doesn't exist. – Adrian Macneil Oct 26 '12 at 23:22
  • I think your question is really about how to use the detector library, so a link to the documentation would be useful :) – Adrian Macneil Oct 26 '12 at 23:24
  • I'm trying to make an EE add-on based on http://detector.dmolsen.com/ I know the property exists, because if I include the library directly in the user_agent function and do `return $ua->ua` I get output. – Tyssen Oct 26 '12 at 23:41
  • I see. That library is a little crazy. I've updated my answer with an example of how best to use it. – Adrian Macneil Oct 27 '12 at 01:04
  • Glad it wasn't just me going crazy then. Your answer solved the problem I had with the original question, but now I have a different one, but still related to the same plugin. Do I add an answer to my own question so I can include a better code sample, or do I need to start another thread? If it's the latter, seems like a bit of a limitation to the SO format to have so many related questions spread out among different threads. – Tyssen Oct 27 '12 at 11:29
  • It's no problem starting a new question if you're having a new problem. The point of stack overflow is to build a searchable database of questions and definitive answers, rather than to have ongoing discussions :) – Adrian Macneil Oct 28 '12 at 02:51
  • Done: http://stackoverflow.com/questions/13114460/outputting-variables-within-a-plugin – Tyssen Oct 29 '12 at 00:21