Object Relational Mapping with PHP and MySQL
|Custom session h...||Aug 2, 2008|
|Writing maintain...||May 8, 2008|
|Using PHP's Soap...||May 6, 2008|
|References in & PHP||Feb 18, 2008|
|A Quick Guide To...||Feb 11, 2008|
|Evil Eval||Feb 2, 2008|
|Implementing the...||Jan 23, 2008|
|Object Relationa...||Jan 20, 2008|
|Recursed Iterators||Jan 17, 2008|
|Memcache: the so...||Jan 5, 2008|
|My Favourite Com...||Dec 17, 2007|
|Overloading in P...||Dec 16, 2007|
|Exporting a Simp...||Dec 16, 2007|
|There is a race ...||Dec 13, 2007|
|Ajax and UTF-8: ...||Dec 12, 2007|
|Dear FragFrog, You..||Jan 12, 2013|
|I've taken a look ..||Mar 30, 2012|
|Another interestin..||Feb 17, 2012|
|I am also using Ap..||Jul 21, 2011|
|Yeah extension_dir..||Jul 21, 2011|
|You're sure the li..||Jul 20, 2011|
|I've downloaded se..||Jul 20, 2011|
|As someone recentl..||Mar 29, 2011|
|Thats actually qui..||Jul 19, 2010|
|Peculiar, it works..||Jul 13, 2010|
|Doesn't work with ..||Jul 12, 2010|
|Don't tell anyone,..||Jul 5, 2010|
|Yeah. "Paint" does..||Jul 5, 2010|
|Woops, fixed that,..||May 19, 2010|
|It's catEgory, not..||May 18, 2010|
Just the mere useage of classes does not merrit all; in fact, it can be a disadventage when used improperly as opposed to procedural coding. I like to think there are two kind's of programmers: those who write object oriented code, and those who use classes.
Not so long ago I definately belonged in the later catagory, so as anyone set on selfimprovement would do I figured I'd get some good training to fix that. So, I decided to write a ORM system - not an easy task, and definately impossible to do without a really good use of objects.
Of course it is all still in the preliminary stages, yet selecting and updating rows already works quite well:
Code (php) (nieuw venster):
// Get the 'menu' table from our database:
$menu = $context -> db -> menu;
// Get the record with ID 5:
$menu -> id = 5;
// Retrieve the record and output it's name:
echo $menu -> name; // Echo's: 'Register'
// Get the record with ID 2:
$menu -> id = 2;
// Retrieve the record and store it in an object 'name':
$name = $menu -> name;
// Set a new value for this record's 'name' column:
$name -> name = 'Logout';
// Update the record in the database:
$name -> update();
// Retrieve the name again - this time the updated
// value is returned.
echo $name -> name; // Echo's: 'Logout'
The most difficult part about this for me was being able to 'call back' - a result object needed to know what his parent was so he would be able to execute updates on records with the same limitations. In addition, if the parent's parameters were changed the resultobject should perform updates with these new parameters. This meant using a lot of references, never an easy task, and in addition preventing recursiveness that would consume server resources at ten times the really required rate.
When that's all said and done the system also needs to function without a minimum of effort on the programmers part, and to that end I opted to use __call methods to allow virtually any default PHP function to work on the results:
Code (php) (nieuw venster):
// Retrieve the menu table:
$menu = $context -> db -> menu -> get();
// For each item, echo the name:
while ($name = $menu -> each())
echo $name -> name;
// Want the first item? No problem!
echo $menu -> first() -> name;
// Or the ID of the next one? Easy!
echo $menu -> next() -> id;
I've been implementing a lot of these small options over the past two days and the result is starting to 'feel' quite usefull, yet it still has not seen any reall production work - which as always tends to be the 'real' testing ground. Currently there are 9 different classes required to do this, forming about 350 lines of code, and undoubtedly there are many more to come, so one could argue that just writing the original project could have been halfway done by now as well. Still, as I said originally, it's one hell of an exercise into OOP :)