I’m a fan of procedurally based controllers.  They make sense, they use less memory, run faster, are easier to debug, and most importantly, are easier and faster to create.

Let’s say you have a user-account page.  It allows the user to change his/her email address, password, physical address and other similar parameters.  I’m going to assume the file structure looks something like this:

  • my_account.php
  • my_account_save.php
  • account_class.inc.php

Using the command structure, let’s assume that my_account.php has basically one command, it lets the user view their own account details and shows those details in a <form> view.  The my_account_save.php page accepts inputs from my_account.php and tells the model, account_class.inc.php, what to do with them.

If you’re using an object-based controller, your files might look like this:

  • my_account.php
  • controller.inc.php
  • account_controller.inc.php
  • account_class.inc.php

In the procedural version of this code the my_account.php and my_account_save.php are very closely linked.  The fieldnames in my_account.php are used in my_account_save.php and tell the model what data to put where.  It then tells the model to save its work and bring the user back to the my_account.php page with any notifications necessary.

In the object oriented version of this system, the controller.inc.php page must determine what kind of input is coming in from which system (excess code), then spin up a copy of the correct controller object and pass the form variables to it.  The controller must then decide what action is being done (setting email, resetting a password?) and then take appropriate action.  It then returns results or failures back to the controller.inc.php file which then needs to know to redirect the user back to my_account.php with successes or failures.

Besides having the extra (unnecessary) layer of abstraction in the object-based system, I also don’t like that form variables that are created in my_account.php are tied so tightly to the account_controller.inc.php.  If you’re going to be abstracting your code out, then you need to be passing each layer copies of the data, modeling into whatever format the next layer up (or down) is expecting.  In many cases, that’s just excess work for what you actually need to do to get the project done and creates more code than is necessary.  More code equals more bugs and more time.

March 29th, 2013

Posted In: Basics

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: