Welcome Guest, Not a member yet? Register   Sign In
Cannot load libraries in controller methods, only constructor
#1

[eluser]SubnetZero[/eluser]
Hello all,

I've got this really weird condition where I can load libraries (in fact anything) in my controller's __construct() method and the corresponding library (helper, etc) is loaded successfully. However, if I load the library (etc) in a method, then the object is not available.

This works:
Code:
Class MyController extends Controller
{
  function __construct()
  {
    parent::Controller();
    $this->load->library('validation');
  }

  function someMethod()
  {
    print_r($this->validation);
  }
}

This does NOT work:
Code:
Class MyController extends Controller
{
  function __construct()
  {
    parent::Controller();
  }

  function someMethod()
  {
    $this->load->library('validation');
    print_r($this->validation);
  }
}

This does not seem to be consistent with the documentation nor the examples I'm seeing. Has anyone else seen this behavior?

The exact error message I'm getting is: Undefined property: MyController::$validation
#2

[eluser]imzyos[/eluser]
the two ways works fine for me, php4 and php5 =S
#3

[eluser]SubnetZero[/eluser]
Well, I'm certain that it does. Just as I am certain that it's something I've done, I'm just having a bear of a time figuring out what it might be.
#4

[eluser]SubnetZero[/eluser]
Here's another clue...

This works fine:
Code:
Class MyController extends Controller
{
  function __construct()
  {
    parent::Controller();
  }

  function someMethod()
  {
    $this->load->library('validation');
    $abc =& get_instance();
    print_r($abc->validation);
    //print_r($this->validation);
  }
}
#5

[eluser]tonanbarbarian[/eluser]
Have you tried using the PHP4 constructor type method instead
I have seen a few other posts like this and I think that generally solves the problem

Code:
Class MyController extends Controller
{
  function MyController()
  {
    parent::Controller();
  }

  function someMethod()
  {
    $this->load->library('validation');
    print_r($this->validation);
  }
}
#6

[eluser]SubnetZero[/eluser]
Yep, just tried it, no joy. It's really very odd. I've tracked the series of statements and everything APPEARS to be going as expected (validation is being loaded into the CI instance), it's just that $this doesn't seem to be updated. It almost acts like a scoping issue, which I would expect in JS, not in PHP Smile

Adding the loader class method call to the constructor (PHP4 or 5 style) causes everything to load properly. Using it within the method itself causes it to NOT be added to $this, which is crazy since $this should be the same object in the constructor as the method, no?
#7

[eluser]tonanbarbarian[/eluser]
Is the example code above the exact code you are using or is it just a summary?
I ask because there may be something else in your code that is causing the problem
If not could you please post the actual code

Some things that could cause problems...
- A method in your controller called 'load'
- Other libraries you are loading could be causing a conflict
#8

[eluser]SubnetZero[/eluser]
Here is the exact code:
Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $this->load->library('validation');
        print_r($this->validation);
        die();
        $this->load->view('welcome_message');
    }
}
?>

The only thing in autoload.php (other than the standard $autoload['xxx'] = array(); where xxx is stuff like config, language, etc.):
Code:
$autoload['libraries'] = array('session', 'database');
#9

[eluser]SubnetZero[/eluser]
So apparently this has only happened to me. I'll do some digging and see if I can resolve this issue, I had hoped that someone else had seen something similar to this.
#10

[eluser]tonanbarbarian[/eluser]
I have seen a number of other posts talking about issues like this and in each case it was an issue with a method or variable already existing in the controller or the library that was conflicting.
I have never had a problem loading the validation library

I suggest you turn on debugging
config/config.php
Code:
$config['log_threshold'] = 2;
$config['log_path'] = '/some/where/valid/';
and then look at the logs to see what they show you




Theme © iAndrew 2016 - Forum software by © MyBB