CodeIgniter Forums
& get_instance() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: & get_instance() (/showthread.php?tid=69062)



& get_instance() - tiagocosme.ti - 10-03-2017

good morning riends...

I'm not going to work with CI Hook. I'm trying to import "= & get_instance ()" but to no avail. I'm using version 3.1.6.

What can it be?


<?php

class Login
{
    public $CI;
    
     public function __construct()
     {
            // Assign the CodeIgniter super-object
        $this->CI =& get_instance();
     }

    function validar()
    {
        echo "validar";
        $this->CI->load->helper('url');
    }


error:

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: hooks/Login.php
Line Number: 16
Backtrace:
File: C:\xampp4\htdocs\crm-radiomar\application\hooks\Login.php
Line: 16
Function: _error_handler
File: C:\xampp4\htdocs\crm-radiomar\index.php
Line: 315
Function: require_once

Fatal error: Call to a member function helper() on null in C:\xampp4\htdocs\crm-radiomar\application\hooks\Login.php on line 16
A PHP Error was encountered
Severity: Error
Message: Call to a member function helper() on null
Filename: hooks/Login.php
Line Number: 16
Backtrace:


Thanks!
Tiago Cosme


RE: & get_instance() - InsiteFX - 10-04-2017

If your not going to use hooks then why is your Login.php Library in the hooks folder?

Also it depends on whats been loaded see Codeigniter.php and Loader.php in the ./system folder.


RE: & get_instance() - Shawn - 10-08-2017

(10-04-2017, 02:00 AM)InsiteFX Wrote: If your not going to use hooks then why is your Login.php Library in the hooks folder?

Also it depends on whats been loaded see Codeigniter.php and Loader.php in the ./system folder.

You can put your class in the Models directory or a subdirectory of Models. You will need to include it in your controller or in whichever model class is using the Login class.
Code:
defined('BASEPATH') OR exit('No direct script access allowed');
include_once(APPPATH.'models/Login.php');

class Login_controller extends CI_Controller {
...
Using composer you can store classes in a completely different directory, but the include_once is a simple way to start with non-singleton classes.