I'm using Codeigniter PHP Framework for developing a web application, and when developing an admin zone, I've ended building a generic parent class called AdminController which is extended by the child classes to develop my Admin zones.

I've shortened my code to show what I really want to show, my code is something like:

Parent Class:

class AdminController {

protected $url_action   = null;
protected $permissions  = null;
protected $formulario_gestion  = null;

function __construct() {
    if ( empty( $this->permissions ) ) {
        throw new Exception( 'No se ha definido permissions' );
    if ( empty( $this->url_action ) ) {
        throw new Exception( 'No se ha definido la url_action' );
    if ( empty( $this->formulario_gestion ) ) {
        throw new Exception( 'No se ha definido el formulario_gestion' );

public function create() {
    if ( ! $this->checkPermissions( $this->permissions, $this->user ) { 
        redirect( base_url );  

    $formulario = new $this->formulario_gestion( null );
    $formulario->setFormValues( $this->get_update_values() );

    // More code below...

// Doing this to make sure I'll overwrite it
public function get_update_values() {
    throw new Exception('No has definido get_update_values');

// More Methods and things...

And then, I use my child class to initiate all the values required in the parent, overwrite all the functions I need have to be overwritten, and go:

Child Class

class Webs extends AdminController {

function __construct() {

    // Initiating parent values:
    $this->permissions = 'webs';
    $this->url_action  = strtolower( __CLASS__) ;
    $this->formulario_gestion = 'FormularioWebs';


// Overwriting what I need to be overwritten
public function get_update_values() {
     $values = $array(
         'field1' => 'value1'
     ,   'field2' => 'value2'
     return $values;

And this work quite well: All the pages share the same behaviour and I only write relevant code.

My problem is: This is a nightmare when building a whole CRUD with it: I ended up with many properties I only use in some methods, and I don't know if there is a "standard" or correct way of doing it. I mean what are the flaws of doing this in this way? It is correct?

I don't like having so many properties laying out there that are only going to be used in a method, and I don't know how to encapsulate them in a way only the method use it it'll have it. What I ended up is something like the code bellows, but I don't really like it, as you don't know in the parent where the value is declared.

Method in Parent Class:

public function show() { 
    echo $this->var;

Method in Child Class:

public function show() {
     $this->vars = 'value'
  • @gnat, thanks for the editing, I didn't know about avoiding regrets in questions, XD – Chococroc May 14 '14 at 18:38
  • If you have something called FooController, odds are that shouldn't be a class, just plain old functions. And when you do need a class, implementation inheritance is almost always a bad idea. – Doval May 14 '14 at 19:22
  • @Doval, no, it's not odd. Codeigniter uses MVC, with a singleton pattern for it main class. The code above are my controllers, which are classes that extends the core class. And my problem is that I almost have all the time the same CRUD behaviour with the same methods. If I had to write a function for all of them, I'd be crazily copy-pasting the same code. – Chococroc May 15 '14 at 6:44
  • Ah, well, that makes sense, since I consider singletons and MVC to be a cargo cult antipattern. If you have nearly-identical code repeated multiple times, you can just use functions to avoid the duplication. Of course, I don't know how Codeigniter expects you to do things, so it may be a moot point. – Doval May 15 '14 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.