1

I'm trying to load an external library into an ExpressionEngine plugin but am getting:

Message: Undefined property: Detector::$EE

In the plugin itself I've got:

public function __construct()
{
    $this->EE->load->library('detector');
    $this->EE =& get_instance();
}

and my folders are set up like:

detector
-libraries
--Detector.php
-pi.detector.php

What am I doing wrong?

Having moved past the loading library error, I'm now getting an 'undefined variable' error with the following code:

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

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

That's if I have {exp:detector:user_agent} in my template. If I {exp:detector} I get no output.

Tyssen
  • 1,569
  • 16
  • 35
  • It would be better to create a new question, since your second problem is unrelated to your first (and if I answer below it wouldn't be a solution to your original problem). However, to answer your question, you need to use `$this->return_data = ""` in your constructor: http://expressionengine.com/user_guide/development/plugins.html#two-segments – Adrian Macneil Oct 26 '12 at 22:43
  • That's producing the same result. I'll start a new question. – Tyssen Oct 26 '12 at 22:55
  • And here it is: http://stackoverflow.com/questions/13095563/undefined-variable-error-in-expressionengine-plugin – Tyssen Oct 26 '12 at 23:05

1 Answers1

7

you should change your code like this:

$this->EE =& get_instance();
$this->EE->load->add_package_path(PATH_THIRD.'/detector'); 
$this->EE->load->library('detector');

First initialize the $this->EE variable, then you can load the library. So in this case it would be

$this->EE->detector->user_agent();
LondonRob
  • 73,083
  • 37
  • 144
  • 201
fccotech
  • 449
  • 2
  • 7
  • Thanks, that's made that error go away but I'm having trouble now actually loading the library. With what I have above, I get an 'undefined variable' error when trying to access one of the variables the library creates. I tried changing the loader to: $this->EE->load->library('libraries/detector'); but that gives me *Unable to load the requested class: detector*. – Tyssen Oct 22 '12 at 12:07
  • 1
    I just edited the previous answer to add the code needed to tell EE about your library location. – Derek Hogue Oct 22 '12 at 12:13
  • You shouldn't need to call add_package_path(). Just use load->library('lib_name'), and make sure it's placed in your add-on's `libraries` directory (you don't need to specify the `libraries/` part when you call load). – Adrian Macneil Oct 22 '12 at 19:58
  • That's what I'd read elsewhere too Adrian but I've got a libraries directory inside my add-on directory. And adding the add_package_path is giving me the same 'undefined variable' error. :? – Tyssen Oct 23 '12 at 22:43
  • What do you mean "one of the variables the library creates"? Perhaps you could update your question above with the new information, or consider starting a new question? – Adrian Macneil Oct 24 '12 at 07:11