1

I was creating objects of a class in Codeigniter core classes which I will extend in my controllers but there seems to be problem with loading model after object is created. Here is what I did

Core File:

require_once('folder/systemize.php'); // class systemize is in this file

class MY_Controller extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $systemize = new systemize();
        $systemize->init();
    }

After this object is initialized, loading model will not work

Controller file which extends MY_Controller

    class Log extends MY_Controller
   {
    function __construct()
    {
        parent::__construct();
        $this->load->model('access_model', 'TEST');
        $this->TEST->foo('param1', 'param2'); //function inside access_model
    }

It will give me these errors A PHP Error was encountered

Severity: Notice Message: Undefined property: Log::$TEST

Fatal error: Call to a member function foo() on a non-object

These errors are solved if I don't create object of systemize. Can anyone tell me the how to solve this problem?

EDIT: Systemize File

class Systemize extends CI_Controller
{

    function __construct() {
        parent::__construct();
    }

    public function init() {
        if('2' == '3') // false for now so it does nothing
        {
            $this->load->view("system/view_file");
            exit();
        }
    }

Php error indicates on Log File where TEST->foo is called

If I Load Model before creating object, it works but not vice-versa. I still don't understand why is Codeigniter behaving like this?

7
  • That error message must shows your line number.Write your whole systemize class and indicate the line number too Commented Mar 21, 2015 at 17:43
  • No, don't post your entire code. Post an MCVE and the line number for that. Commented Mar 21, 2015 at 18:02
  • Done. Hope now you will be able to find the problem @QPaysTaxes Commented Mar 21, 2015 at 18:09
  • Where is the method 'TEST' defined? Commented Mar 21, 2015 at 18:34
  • I loaded the model with alias Test. $this->load->model("model_name", "Test"); Commented Mar 21, 2015 at 18:41

1 Answer 1

1

Ok after going through system files of Codeigniter and printing and echoing inside them I found the source of problem. Now the behavior of CI_Controller is that when we extend CI_Controller, it creates object of Controller and Model and stores it with reference to the pointer.

Now what I did is that I extended systemize with CI_Controller. MY_Controller extends CI_Controller which means $obj is created storing pointer to all objects of CI which will be used. But when I manually created object of systemize extending CI_Controller, it has created object of only Controller and not Model and replaced the $obj. That is why loading model will cause problem since $obj doesn't have Model Object in it.

The solution the problem I found is I did not create object of systemize in MY_Controller. Instead I extended MY_Controller with Systemize and it started working flawlessly.

Another solution is if you don't need CI functionality in your class you can create object of class and not extend it with CI_Controller.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.