CodeIgniter Forums
Overriding core classes - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Overriding core classes (/showthread.php?tid=71954)



Overriding core classes - imabot - 10-17-2018

I tryed to override the core controller following this page :
https://www.codeigniter.com/userguide3/general/core_classes.html

I added a new class in /application/core/ 


PHP Code:
<?php
class MY_Controller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        echo 'Hello !';
        die();
    }


I checked, line 374 of /system/core/CodeIgniter.php is called properly :


PHP Code:
require_once APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'


But constructor is not called. What am I doing wrong ?


RE: Overriding core classes - dave friend - 10-17-2018

What's the problem?


RE: Overriding core classes - imabot - 10-17-2018

(10-17-2018, 12:25 PM)dave friend Wrote: What's the problem?

Constructor was not called. 

But I solved the problem myself : I didn't extend my controllers with MY_controller, it was still CI_controller.


RE: Overriding core classes - dave friend - 10-17-2018

Huh... go figure. I've created MY_Controller classes on many occasions without any problem.

Line #374 from Codeigniter.php only makes the class code available, it does not instantiate it.

PHP Code:
require_once APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'

The controller is instantiated on line #518

PHP Code:
$CI = new $class(); 

There's a whole bunch of error checking going on in between most of which results in a 404 if all the pieces cannot be found.

You don't mention getting any errors. Did you?


RE: Overriding core classes - imabot - 10-18-2018

(10-17-2018, 02:37 PM)dave friend Wrote: You don't mention getting any errors. Did you?

That's very kind to you to answer to me. I no longer have any problems or errors : everything is fine now as I explained previously:


Quote:I solved the problem myself : I didn't extend my controllers with MY_controller, it was still CI_controller.

That was my mistake.


RE: Overriding core classes - InsiteFX - 10-18-2018

Just a note here you do not have to name the class MY_Controller, you can name the class whatever you want.
It just needs to be saved with the MY_Controller.php filename.


RE: Overriding core classes - imabot - 10-18-2018

(10-18-2018, 02:49 AM)InsiteFX Wrote: Just a note here you do not have to name the class MY_Controller, you can name the class whatever you want.
It just needs to be saved with the MY_Controller.php filename.

Thank you, very useful.


RE: Overriding core classes - dave friend - 10-18-2018

(10-18-2018, 12:26 AM)imabot Wrote: That's very kind to you to answer to me. I no longer have any problems or errors : everything is fine now as I explained previously:
Quote:I solved the problem myself : I didn't extend my controllers with MY_controller, it was still CI_controller.
That was my mistake.

And my mistake was not understanding your explanation of the problem. Blush

I'm glad to hear it was a simple problem. Sometimes those kinds of "bugs" are the hardest to find.


RE: Overriding core classes - imabot - 10-18-2018

(10-18-2018, 05:30 AM)dave friend Wrote:
(10-18-2018, 12:26 AM)imabot Wrote: That's very kind to you to answer to me. I no longer have any problems or errors : everything is fine now as I explained previously:
Quote:I solved the problem myself : I didn't extend my controllers with MY_controller, it was still CI_controller.
That was my mistake.

And my mistake was not understanding your explanation of the problem.  Blush

I'm glad to hear it was a simple problem. Sometimes those kinds of "bugs" are the hardest to find.

Maybe you could help me with the next one : https://forum.codeigniter.com/thread-71960.html !