Welcome Guest, Not a member yet? Register   Sign In
Personal library issue
#1

[eluser]elmystica[/eluser]
I have a set of views that I load in different controllers. Most of these views also need some stuff from the database, so they interact with a model too. Instead of filling each controller with these methods ( to populate and load the views ), I want to create a library that can be reused everywhere.


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

class Mydisplayer {
    
    var $data;
    var $CI;
    
    function Mydisplayer()
    {
        
        $CI =& get_instance();
        $CI->load->helper('url');
        $CI->load->library('session');
        $CI->load->model('Wsb_model');
    }
    
    function display_header()
    {
        $data['site_url']    = $this->CI->wsb_model->get_site_pref('site_url');
        $data['site_name']    = $this->CI->wsb_model->get_site_pref('site_name');
        
        $CI->load->view('heading', $data);
    }
}
?>


controller Wsb.php
Code:
function wsb()
    {
        
        parent::Controller();
        $this->load->model('Wsb_model');
        $this->load->library('Mydisplayer');
    }
    
    
    function index()
    {
        
            $this->mydisplayer->display_header();
             /* ... */
       }
}

However it tells me that inside the display_header function, I try to get property on a non-object
Quote:Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/Mydisplayer.php

Line Number: 19


I'm sorry, but I don't really understand this problem ...
#2

[eluser]Armchair Samurai[/eluser]
You haven't given $CI a value. To set it, you need to change your constructor:
Code:
class Mydisplayer {
    
    var $data;
    var $CI;
    
    function Mydisplayer()
    {
        
        $this->CI =& get_instance();
        $this->CI->load->helper('url');
        $this->CI->load->library('session');
        $this->CI->load->model('Wsb_model');
    }
// rest of class...
#3

[eluser]elmystica[/eluser]
Thanks,
but that doesn't resolve the issue ...

Edit: nevermind, it should be $this->CI->Wsb_model-> ...

THANKS!




Theme © iAndrew 2016 - Forum software by © MyBB