Welcome Guest, Not a member yet? Register   Sign In
General problems regards loading libraries, and hooks
#1

[eluser]Aquillyne[/eluser]
I seem to have recurring problems whereby I instantiate a library and try to use a function from it - but it just doesn't work. I mean, for instance:

The following is a hook of mine:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sitesession extends Controller
{

    function Sitesession()
    {
        parent::Controller();
        $this->load->library("session");
    }

    function start()
    {
        $this->session->set_userdata("active", TRUE);
    }

    function destroy()
    {
        $this->session->unset_userdata("active");
    }

}

?>

Which just doesn't work. I get 2 errors:

Quote:Undefined property: Sitesession::$session

Fatal error: Call to a member function set_userdata() on a non-object in C:\Server\Frameworks\CodeIgniter\application\hooks\sitesession.php on line 15

Why doesn't this work? I've tried calling the hook at all the different hook-points, but I get the same error. I've tried loading the library in the respective functions, and auto-loading it - still no luck.

Please help!
#2

[eluser]Mirage[/eluser]
Hi,

You're second error is actually just a result of the first one. Since $session is undefined you can't call set_userdata() on it.

So the question becomes 'Why is $session undefined?"

Amony the most likely answers is: Your session library isn't properly loaded. From your code example, things look alright. So I'm not certain why it wouldn't be loaded. what do you get when you add this in your start() method:

Code:
function start() {
    echo '<pre>'; var_dump($this->session);echo '</pre>';
    ...
}

Also - if you haven't done so - enable logging and see if the session reports as being loaded.

Cheers!
#3

[eluser]Aquillyne[/eluser]
I get "NULL" - yes, the load seems to be failing. But it doesn't give me an error message for the fail. I just can't reference the "session" class from "$this", which it seems is the problem?

I should emphasise that the file is a hook, which I have set to be called on post-controller (I've tried setting it in different places, but I get the same error). I've had similar problems in non-hook files too.

Also to note: I haven't tampered with the core CI files at all - I only just downloaded the latest version. All I've done is add this hook and some of my own welcome pages/controllers. (The only other thing I've added is a MY_URI extension to the URI library, which simply contains a direct copy of the original function that produces an error when the URL contains invalid characters - I changed one line so that it gives a 404 error rather an a textual one).
#4

[eluser]Mirage[/eluser]
If this is a hook then it won't work like a controller itself. I don't know if you have any specific reason to extend your hook from the Controller, I wouldn't. Perhaps you did it because you assumed that you would inherit all the properties of the controller - which is not the case.

Try this:

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sitesession
{
    var $CI;

    function Sitesession()
    {
        $this->CI=&get;_instance(); / /grab a reference to the controller
        $this->CI->load->library("session");
    }

    function start()
    {
        $this->CI->session->set_userdata("active", TRUE);
    }

    function destroy()
    {
        $this->CI->session->unset_userdata("active");
    }

}

?&gt;

Hope this helps.
#5

[eluser]Aquillyne[/eluser]
This seems to have worked! The caveat is that it doesn't work if hooked on pre-controller or before. It works on post-constructor-controller. I'm not entirely sure what the difference is, but at least it works! Thanks!
#6

[eluser]Mirage[/eluser]
get_instance() retrieves and instance of the current controller. Therefore it won't work pre-controller because no controller instance has been created at that point. I'd have to check, but I'm pretty sure you could make it work pre-controller by either including and calling the Loader directly in your class or use the public function load_class().

HTH




Theme © iAndrew 2016 - Forum software by © MyBB