Friday, October 21, 2005

My first PHP "framework"

I just wrote my first "framework" in PHP5. Granted it is very, very bare but it marks a radical change from the way I used to develop in PHP (just after the dinosaurs died off and everything still had a .PHP3 extension on it). Now before I get skewered for not doing this in .NET or not using an existing framework or not embracing Java (Java server pages and EJB's) or really following object oriented programming and the MVC methodology strictly to the letter let me just state that I am part systems programmer, part application developer, part database programmer, etc. There is just too much for my brain to process and I like to do things that to me make straight-forward sense.

I started with the controller; which is simply index.php with defines and requires. The required includes are:

  • Init
  • Common functions and classes
  • Templates
  • Actions
  • Done

The init code sets globals and not much else.

The common code will define common functions and classes used by all other nuggets of code.

The template code defines a parent class for common template properties (like title), methods for handling caching generated body output, methods for raw text input, and methods for common manipulation of buffered data.

The actions code handles the requests, dispatching them to separate included files. Some includes will be sub-structured depending upon common requirements. Some actions are ID based, like "show" and "wiki" and "test". Each included module will create one of the child template objects (defined above).

The "done" code will clean up any common messes, which isn't much.

One of the things I do not like about most frameworks written for PHP is they always define classes and variables even if they will never be used during the GET. For example, if no database access is performed then why include the database code to define the classes and variables, or worse yet – make a database connection. The only time this should happen is if every single page generated by the framework has some database element.

One reason why I'm attempting to keep things straight forward and simple is because eventually my host will force me to be limited and simple. Having Apache2 and PHP5 running on my workstation with full access to our Oracle system and SQL servers and mySQL locally (if I want) is wonderful. If I copy this to my own domain, however, I might not have the luxury of even mySQL being available.

The important thing is I learned something and it works for me (so far). All I need to do is "pretty" it up with some CSS and fill in the setHead and setAss methods of the XHTML child class and I'll be happy. Oh well, it's late on a Friday - I should be going home now. Have a nice weekend everybody!

No comments: