Welcome Guest, Not a member yet? Register   Sign In
get_instance from constructor
#1

Im creating a plugin library, and in the constructor, im initiating a local static CI reference, using the get_instance() function. From the Library, I need to be able to hit a model, the issue im having, is if I try to execute a method from a model inside the constructor (Or execute a method in the constructor that executes a method from a model), it fails, however, if I execute the exact same thing from a method thats called from outside the library, it works just fine.

The controller I'm executing this from is Test, and the errors are:
Quote:Severity: Notice
Message: Undefined property: Test::$Plugins_model
Filename: libraries/Plugins_lib.php
Line Number: 33

Line 33 would is notated below..

Code:
PHP Code:
<?php
class Plugins_lib {

    private static 
$CI;

    public function 
__construct()
    {
        
// Codeigniter instance
        
self::$CI =& get_instance();

        
// THIS FAILS...
        
$this->get_active_plugins();

    }

    
// This gets executed externally (From controller)
    
public function active()
    {
        
// THIS WORKS JUST FINE
        
return self::$CI->Plugins_model->get_active_plugins();
    }

    
// This gets executed from the constructor
    
private function get_active_plugins()
    {
        
// FAILS
        
return self::$CI->Plugins_model->get_active_plugins(); // Line 33
    
}
}


Any idea what the issue could be?
Reply
#2

"Undefined property: Test::$Plugins_model". You are trying to call before "self::$CI->Plugins_model" is created.

By the way, what do you really want to do?
In Plugins_lib, "self::$CI->Plugins_model->get_active_plugins()" is the same as "$this->get_active_plugins()" even if
"self::$CI->Plugins_model" exists.
Reply
#3

(This post was last modified: 08-06-2015, 04:30 PM by jLinux.)

(08-06-2015, 02:40 PM)kenjis Wrote: "Undefined property: Test::$Plugins_model". You are trying to call before "self::$CI->Plugins_model" is created.

By the way, what do you really want to do?
In Plugins_lib, "self::$CI->Plugins_model->get_active_plugins()" is the same as "$this->get_active_plugins()" even if
"self::$CI->Plugins_model" exists.

Oh, so CI is loading the library before the model... How can I change that? Or atleast have this work somehow

As far as what i wanted to do. Im just testing it, One works, one doesnt because its in the constructor

I need to be able to call that model from the constructpr
Reply
#4

The only work-around I can think of is put that functionality into another method, then execute the method via a CI Hook... which I hate that idea
Reply
#5

Sorry, I confused Plugins_model and Plugins_lib.

All you have to do is to load Plugins_model before loading Plugins_lib.
Your problem is in your controller code.
Reply
#6

(08-06-2015, 05:18 PM)kenjis Wrote: Sorry, I confused Plugins_model and Plugins_lib.

All you have to do is to load Plugins_model before loading Plugins_lib.
Your problem is in your controller code.

They're both auto loaded. Ill just try to load the model manually
Reply
#7

Load the model in your library, then autoload the library. Don't autoload the model.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB