/PHP/ Object-oriented programming
19/03/2008 | Filed under Develop > PHP

Paul Hudson takes the blood, sweat and tears out of mastering object-oriented programming, a powerful technique that’s otherwise known as OOP
Mention object-oriented programming to most web developers and you usually get the same effect: blood will drain from their faces and their eyes will glaze over. That’s a shame, because OOP is a hugely useful technique in anyone’s toolbox. The reason people are usually afraid of it is because OOP has long been the realm of “serious programmers”, as opposed to “dabblers”. Given that many web developers see PHP as a neat hack to add functionality around the beautiful interface they’ve designed, you can imagine why OOP gives them jitters.
In this tutorial, I want to show you how object orientation works with the minimum amount of theory and the maximum amount of practical, hands-on code that you can get using straight away. So let’s go OOP!
Click here to download the support files
Click here to download tutorial PDF
Comments
Beate / 21/03/2008 / 14:23 / http://www.anwalt-seiten.de
It's high time for web programming to become "serious" - and to take the fear of the "dabblers" - serious programming is paid better.
Since PHP5, imho OOP is a "must" for all PHP developers. Thanks for the tutorial!
omo / 22/03/2008 / 13:37
dont know about anyone else but i couldn't get this to work. not even the first 2 pages would work. i then just uploaded the source pages to see if they would work, but to no avail. maybe its just my host or something i am new to php but i did try to get it to work but alas i had no luck. the article however was well presented and im sure this problem is my fault but anyway thanks for your time mr hudson.
Bobo / 02/04/2008 / 20:32
This tutorial was great, but like omo I couldn't get it to work. I am running PHP 4.3.9. Could it be that something about this code requires PHP5? Here was the result:
<Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /my/pathname/classes.php on line 3.
Any suggestions?
Thanks a bunch.
Viola / 03/04/2008 / 10:39
Just how much am greatful for this tutorial should have known earlier
Olly / 04/04/2008 / 11:10 / http://www.something-simple.co.uk
Nice little tutorial there! Great to get started with OOP!
~to Bobo, yes many of the functions used in this tutorials are PHP5+ only.
Infact, the public and private method calls, and pretty much all of the "__blah" methods are PHP5 only.
If you wanna test out this tutorial, I suggest you get a hold of XAMPP which is a free bundle of software which allows you to run a local server on you rmachine (with Apache and, more importantly, the latest version of PHP). You can download the Installer from http://www.apachefriends.org/en/xampp-windows.html#641.
Great
Olly
Phill / 23/04/2008 / 17:15
The tutorial covers some really basic things; i wanted to say having learned and been practicing OO php for a few months now, its power is epic. From test harnesses, factory db connection wrappers, a page class for robust page structure, html pools, schema synchronisation, abstraction layers, sql statement binding, even small component building to solve common programming problems; the list is endless. My library currently sits in the region of 75 classes (and I'm a relative novice, there will be tons more). The great thing is, the more components you build, the easier your job becomes because your not programming, but bolting components together. Do it nice or do it twice!!! Learn OO folks!
John Knowles / 10/07/2008 / 10:05 / http://www.knowj.com
I taught myself OOP within the PHP5 environment just over a year ago and have noever looked back first. I first worked with the limited support PHP4 offers then quickly moved onto the far superior PHP5 learning about patterns and the features it offers such as control over what can access methods and objects.
OOP is a must have skill for any serious web developer the days of spending hours creating a simple input form with validation to upload to a database are over once you peice together a decent framework for your self.
It would be nice to see some more tutorials/articles within .NET with an OO focue pushing the dabblers into the real world of web programming
Alex / 11/07/2008 / 09:10 / http://www.overgroepsreizen.nl
OOP in PHP can is a really good way to bring structure to your code, but it's also a greatt way to get completely lost. Having experimented a lot with MVC and OOP, my experience is this: if you really want to get to know the power of OOP, then DO NOT start building those complicated structures that do everything you ever dreamt of in only 3 lines of code.
Try this: start modeling your code MVC like, without the MVC and without the OOP. It's really the best way learn both. Write procedural or functional that puts all data in an array or an object. Use this object as the source of data for the HTML code which you put at the end of your script. FORCE yourself not to put A SINGLE HTML tag in the part of your code that generates the data, and force yourself not to put a single line data-generating code in your HTML. Just conditional statements and loops (if, for, foreach) and some string operators should be enough for the HTML. This way you are following some of the most essential basic rules of OOP and MVC and you can teach yourself the way of thinking.





