Welcome Guest, Not a member yet? Register   Sign In
Problem with constructor in library
#1

[eluser]CIfan1000[/eluser]
Hi guys!

I have the following simple library, it works fine without the constructor but when I put in the constructor I get:

Quote:Fatal error: Cannot access parent:: when current class scope has no parent in C:\Backup\Apache2.2\htdocs\CodeIgniter\system\application\libraries\Myauth.php on line 15

Any help would be very much appreciated!

Code:
<?php

    if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Myauth
    {

        // Create constructor to load CI libaries and helpers:
        function Myauth()
        {
            parent::Controller();
        }


        function check_if_logged_in()
            {
                $CI =& get_instance();
                $CI->load->library('session');

                // Redirect user to login page if user is not authenticated:
                if ($CI->session->userdata('UserStatus') != "Authenticated")
                    {
                        $CI->session->set_flashdata('title', 'Not logged in');
                        $CI->session->set_flashdata('message_title', 'Not logged in');
                        $CI->session->set_flashdata('message_body', 'In order to use this page you need to log in.');
                        redirect('message/message');
                    }
            }
    }

?>

Thanks!
#2

[eluser]Pascal Kriete[/eluser]
Libraries do not extend the controller, so you don't need that line.
#3

[eluser]CIfan1000[/eluser]
Hi,

I am sorry but I don't quite understand - I am fairly new to constructors.

The manual doesn't seem to explain constructors within a custom library and I have searched the forum and not found anything that helps me.

I currently have the lines:
Code:
$CI =& get_instance();
                $CI->load->library('session');

In my function check_if_logged_in() and this works fine.

However, I would like to have multiple functions in this library and so I would like to load the session library in a constructor so it is available for all functions in the library.

I took the
Code:
parent::Controller();
line out of the constructor and put in the session libary load lines into the constructor:

Code:
// Create constructor to load CI libaries and helpers:
        function Myauth()
        {
                $CI =& get_instance();
                $CI->load->library('session');
        }

This also gives me errors - sorry that I don't understand your instructions.

Am I mistaken thinking that I can use a constructor in a library?

Am I mistaken thinking I can load the session library in the constructor and have it available for all functions in the library?

Or do I need to load the session library in each function?


Thanks!
#4

[eluser]dmorin[/eluser]
You only need to load the session library once, whether you do it in the controller or library doesn't matter. Instead of doing this in your constructor:

Code:
$CI =& get_instance();

do this

Code:
$this->CI =& get_instance();

That way, you can access anything like you normally would using $this->CI->session instead of $this->session.




Theme © iAndrew 2016 - Forum software by © MyBB