spreaderman
spreaderman

Reputation: 1088

Codeigniter, problems with extending classes

I have the following simplistic code:

// FILE: controllers/Top.php

class Top extends MY_Public_Controller {
        function __construct() {
   }
   public function Top() {
        echo 'Hello';
   }
 }

// FILE: application/core/MY_Public_Controller.php

 class MY_Public_Controller extends MY_Controller {
        function __construct() {
               parent::__construct();
        }
 }

// FILE: application/core/MY_Controller.php

 class MY_Controller extends CI_Controller {
        function __construct() {
               parent::__construct();
        }
 }

And I get the following the following error:

 Fatal error: Class 'MY_Public_Controller' not found in 
 /var/www/example.com/public_html/application/controllers/Top.php on line 5
 A PHP Error was encountered
 Severity: Error
 Message: Class 'MY_Public_Controller' not found
 Filename: controllers/Top.php
 Line Number: 5
 Backtrace:

Any help would be much appreciated!

Upvotes: 1

Views: 435

Answers (2)

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

Try like this...

  1. You can create any number of controller but create controller's must be included in the controller that extends CI_Controller.As Below:

  2. Controller's Name and function Name Keep different (Good Way)

MY_Controller:application/core/MY_Controller.php

 class MY_Controller extends CI_Controller {
        function __construct() {
               parent::__construct();
        }
include_once('MY_Public_Controller.php');// include here
 }

MY_Public_Controller: application/core/MY_Public_Controller.php

class MY_Public_Controller extends MY_Controller {
        function __construct() {
               parent::__construct();
        }
 }

And Top: application/Top.php

class Top extends MY_Public_Controller {
        function __construct() {
   }
   public function index() {  //function name must be different than controller's name
        echo 'Hello';
   }
 }

Upvotes: 1

Jonathan Machado
Jonathan Machado

Reputation: 522

Instead of you create a new file (MY_Public_Controller.php) to create the class My_Public_Controller.

Insert this class inside the My_Controller.php file.

In that way the My_Controller.php file will be like:

class MY_Controller extends CI_Controller {
        function __construct() {
               parent::__construct();
        }
 }

class MY_Public_Controller extends MY_Controller {
        function __construct() {
               parent::__construct();
        }
 }

After I see another answer

Or you can make something like #Hikmat Sijapati said, but instead of you put the require_once, inside the My_Controller.php. Try to put it in the My_Public_Controller.php using 'My_Controller.php' as parameter. Something like that:

My_Public_Controller.php:

include_once('My_Controller.php');
class MY_Public_Controller extends MY_Controller {
        function __construct() {
               parent::__construct();
        }
 }

I have not tried it that way, but I think it will work.

Upvotes: 1

Related Questions