SiteCrafting Blah Blah Blog
Nov. 8, 2006 at 1:36pm
The Joys of Object Oriented PHP
It's pretty common for PHP developers to make complex and difficult to maintain scripts, and I am no exception. I write my code in two distinctly different ways: scripts that do a whole bunch of things depending on input, and classes that do a bunch of things depending on how they are called, but from lots of different scripts. But which is better?
It's a nice ego boost for any prrogrammer to know they created a script/function/class that is really useful, and well designed, but at some point the code stops being well designed, and becomes gross. I think that point is when you have one script that displays an entire (SQL) table of data, views a particular row, edits that row, can delete that row, or can add new row to the table.
If my Software Engineering professor sees me doing that, I imagine that he'll reevalute my grade in his class.
The first version of my coding style (a script that does everything), generally looks like this (psuedo code for the sake of brevity): check permissions
if edit request sent
create edit sql
query database
if delete request sent
create delete sql
query database
if add request sent
create add sql
query database
include header html file
if adding a new row
display blank html form
elseif editing a row
display html form with values filled in
elseif viewing a row
display row preview
else
display all rows
include footer file
Some of you out there might be copying that down to use in your own code, and I don't mind at all. That kind of structure is easy enough to work with when you only have four or five columns for data, but as you get more and more complicated data structures, and incorporate more and more tables into this style, using files like this can get extremely difficult.
There is a better alternative, and that is using Object Oriented PHP. You create a control class for each table of data, and call its seprate methods to perform the tasks you want to accomplish. CodeIgniter gets this design spot on, as each class and function is also translated into a url. For example, if you have a Library class and you want to call the preview method, you would simply point your browser to www.mysite.com/library/preview.
My designs aren't quite as good as CodeIgniter's, but they are good (the reason that CodeIgniter is able to have such good url's is due to some pretty complex File Not Found processing). Here's an overview of my classes:class something
variables according to the columns in the table
function load (id)
queries database for the row that has the specified id
assigns the values to the classes' variables
function save ()
takes the variables from $this and creates an sql statement
and so on as I need. For each different script, I name it something_save.php or something_view.php, where the first word is the class, and the second is the function name. Somethings I leave up to the php file, like xhtml templates and style, but all I have to put in those files is two lines of PHP code: load() and the function call. This makes the control logic of components of your application really easy to use, and also is better from a Software Engineering view.
I hope you can implement this methodology into your own coding, and I welcome comments on how to improve this idea.
Disclaimer: I left out a lot of details on the psuedo code, for several reasons, but mainly because I wanted to talk about a concept, rather than a tutorial.
Posted in Coding Techniques, Critiques, PHP by Dave Poole
Comments (1)
I've only recently started doing some heavy backend work in php, but i've actually used a combination of your methodologies already.
What i find, and will probably go and look for or create, are some utility functions to speed up the presentation. So after i've done all my processing etc, if i have to do code that maybe display's a recordset, i could registerHead(* code that stays static "div id='container'" *), registerBody(*code that has some tags that represent coloum variables along with html markup "{userid}" *) and the footer code. Then you can probably do some processing etc.
Integrate that with a php mysql class and you could have a pretty nice framework for doing database work. Just my thoughts.
Regards,
Kenneth
1 | Left by Kenneth Parris | Dec. 8, 2006 at 11:27am