CodeIgniter User Library Error: Calling a Member Function for a Object

I am trying to create my own library that will contain functions that will be available through the entire site. Inside /application/librariesI made a new file CustomFuncts:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * @brief CommonFuncts      
 * This class is used to provide common user functions to the whole application.
 *
 * @version 1.0
 * @date May 2012
 *
 * 
 */
class CommonFuncts extends CI_Controller {

 protected $ci;

/**
 * function constructor
 */
function __construct()
{
    $this->ci =& get_instance();
}
 /**
* @brief checkCookie
* 
* Checks for a previous cookie, and if exists:
* @li Loads user details to CI Session object.
* @li Redirects to the corresponding page.
* 
*/
public function verificaCookie(){
    $this->ci->load->view('index');
}
 }
/* End of file CommonFuncts.php */
/* Location: ./application/libraries/CommonFuncts.php */

And in the controller welcome.php:

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

class Welcome extends CI_Controller {

/**
 * Index Page for this controller.
 *
 * Maps to the following URL
 *      http://example.com/index.php/welcome
 *  - or -  
 *      http://example.com/index.php/welcome/index
 *  - or -
 * Since this controller is set as the default controller in 
 * config/routes.php, it displayed at http://example.com/
 *
 * So any other public methods not prefixed with an underscore will
 * map to /index.php/welcome/<method_name>
 * @see http://codeigniter.com/user_guide/general/urls.html
 */
public function index()
{
    $this->load->library('commonFuncts');
    $this->commonFuncts->verificaCookie();  
}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

I get the following error message:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$commonFuncts

Filename: controllers/welcome.php

Line Number: 23

Fatal error: Call to a member function verificaCookie() on a non-object in
/var/www/vhosts/unikodf.com/httpdocs/application/controllers/welcome.php on line 23

I tried a lot of things, including extending the library CI_Controllerand using $this->load->view' instead of$ this-> ci -> load-> view ', but still I get the same message,

+3
source share
5 answers

If your library is expanding CI_Controller. This means that you are actually expanding the functionality of the controllers. Instead, declare your library as follows:

class CommonFuncts { }

CI_Controller, , , MVC. , , , .

, :

$this->load->library('CommonFuncts');
$this->commonfuncts->verificaCookie();

, , :

// The second argument is the optional configuration passed to the library
// The third argument is the name with which you would like to access it
$this->load->library('CommonFuncts', '', 'common');
$this->common->verificaCookie(); //  /\ = configuration
//     /\/\/\ = name
+10

controllers libraries.

CI_Controller. , .

-, , :

$this->load->library('CommonFuncts');
// The "CommonFuncts" library is now ready to use
$this->commonFuncts->verificaCookie(); 
+4

( 2.1.3):

//echo CI_VERSION;  to get the code igniter version
Plesae use
$this->commonfuncts->verificaCookie(); //f is small here commonfuncts
instead of
$this->commonFuncts->verificaCookie();
0

. :

$this->ci =& get_instance();  //This is invalid syntax. 

codeIgniter codeigniter

class exampleClass{
    protected $codeIgniter;

    public function __construct()
    {
        $this->codeIgniter =& get_instance();
        $this->codeIgniter->load->helper('url');
        $this->codeIgniter->config->item('base_url');

    }
}
0

All Articles