Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7820015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:08:57+00:00 2026-06-02T07:08:57+00:00

A lot of frameworks out there decided to use this approach: force the user

  • 0

A lot of frameworks out there decided to use this approach: force the user to extend a base controller class (if you want to create a new controller) or to extends a base model class (if you want to create a new model).

Let’s take a look at the code of CodeIgniter’s controller base class:

/**
 * Constructor
 */
public function __construct()
{
    self::$instance =& $this;

    // Assign all the class objects that were instantiated by the
    // bootstrap file (CodeIgniter.php) to local class variables
    // so that CI can run as one big super object.
    foreach (is_loaded() as $var => $class)
    {
        $this->$var =& load_class($class);
    }
    $this->load =& load_class('Loader', 'core');

    $this->load->initialize();

    log_message('debug', "Controller Class Initialized");
}

What does it do? Well, as far as I can see, it just allows us to use $this->load->... for example.

Let’s take a look at the __get() magic method of the model base class:

/**
 * __get
 *
 * Allows models to access CI's loaded classes using the same
 * syntax as controllers.
 *
 * @param   string
 * @access private
 */
function __get($key)
{
    $CI =& get_instance();
    return $CI->$key;
}

It does exactly the same thing. Now what does this way of doing things bring?

PRO

  • You can access useful CI classes by $this->....

CONS

  • You have to force the user to extends the base class
  • You have to force the user to call the parent::__construct() in the class construct
  • get_instace() is reserved
  • $this->instance redefinition cause a fatal error
  • You have basically repeated the same code both in the Model base class and the Controller base class

Now let’s take a look at another approach:

Create a static class, such as App that do all the things the base controller does:
For example, $this->load->... would be App::load->....

Now consider pros and cons again:

PRO

  • You can access useful CI classes by App::....
  • You don’t have to force the user to extends the base class
  • You don’t have to force the user to call the parent::__construct() in the class construct
  • no methods name or properties name are reserved
  • You can use App both in the Model and in the Controller

CONS

  • You have no more the $this-> sexy syntax???

QUESTION

Here it comes the real question: would be the second a better or worse approach compared to the CI one? Why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:08:57+00:00Added an answer on June 2, 2026 at 7:08 am

    PRO

    • You can access useful CI classes by App::….
    • You don’t have to force the user to extends the base class
    • You don’t have to force the user to call the parent::__construct() in the class construct no methods
    • name or properties name are reserved

    This not entirely valid. CI never force dev to extend the base class. All core framework functionality could be easily extended. You can have MY_Controller.php within application/core folder, contain your own base class, eg:

    Front_Controller extends CI_Controller{
      // Share common properties or functionalities across front/public controllers here
    }
    
    Admin_Controller extends CI_Controller{
      // Share common properties or functionalities across administrative controllers here
    }
    

    Then, parent::parent_method() is very common in PHP. Mostly you’ll have this syntax elsewhere, if you really use OO design in your application. This enable you to adding functionality to a subclass without loosing the inherited functionality from parent class.

    So answering your question :

    Here it comes the real question: would be the second a better or worse
    approach compared to the CI one? Why?

    Both attemps can be considered legal, atm. Because, the fact that : 1) there is no consistency checking (something like instanceof CI_Controller in PHP 5, or is_a for PHP4) within CI bootstrap, 2) And, CI not force you to returning anything from a controller action method (a Response object, like in SF, for example).

    Thats to say, you can have an arbitrary class act as a controller. In fact you didn’t need to wrap core Controller functionality within a static class, no one stoped you to use get_instance()->load->library('foo') and get_instance()->load->database() within those arbitrary class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are a lot of web application frameworks out there, based in Java/Ruby/Python/PHP amongst
Are there any lightweight frameworks out there for this task. I have a collection
There are quite a lot of unittesting frameworks out there for .NET. I found
I'm not a huge fan of the JavaScript frameworks that are out there today.
There is a lot of mismash of information out there regarding which version of
This is a bit of a crazy question, but does anyone out there know
I am evaluating some web application frameworks out there, and finally, two of the
There's a lot of information out there regarding installing the ruby/rails mysql gem on
A lot of web frameworks have a standard setup for generating forms with auth
I'm thinking about frameworks. I know they make life a lot easier, but as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.