/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
Bookmark with:
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.
Phill / 26/07/2008 / 12:55
I do agree with Alex; MVC architecture is hugely important and it really is the sensible thing to not jump into OO head first. Scalability, maintainability and context is everything.
I personally learnt the theory of object orientation years even before I embraced a technology that implemented it, and even then, its no easy task.
Tom / 29/08/2008 / 05:30 / http://charitii.com
Nice tutorial! I developed my last project using OOP + PHP and loved it. It really is an excellent way to organize everything. It saved me a lot of hassle. As PHP projects get more complicated OOP will become a necessity.
Matt / 29/08/2008 / 10:06 / http://www.elitespeeddating.co.uk
I am having a hard time getting my head around this. This is probably the best tutorial I have came across, but I still don't get it. I read there are around 700 functions to use be used in PHP 5 OOP, I mean, how is it humanly possible to learn about all these?
Ok, I will go over this a few times and try and impliment it into my own solutions, perhaps as someone mentions, a form framework to validate etc... There is already too much to learn though with everything else!?!?! Why arn't web designers paid as much as doctors????
James / 29/08/2008 / 16:19 / http://www.bigdaydigital.co.uk
The tutorials in .net are really good. I've been following the CSS tutorials and now I'm exploring further. These are really good and I'm going to keep at it. Thanks.
Maikel / 17/09/2008 / 15:43 / http://www.htmleando.com
Thank you for this tutorial about OOP. I'm using a php framework that uses these concepts of object-oriented programming.
Chris / 03/11/2008 / 13:08
Having recently landed my first job as a web developer I thought it would be crucial that I take my first steps into OOP to survive in this industry. After working on some some non-OOP implemented projects I'm starting to appreciate the concept even more. I think this tutorial has been a good basis for me to further my development skills, I remember trying to learn OOP around 4 years ago and it completely went over my head, after working with functions and appreciating their use in the last 18ish months, OOP was making far more sense, many thanks!
Christine* / 14/08/2009 / 10:15
Couldn't get the page title to work until I found a mistake in the tutorial:
> The final change to make constructors work is to pass in the variable in index.php,
> so change the new Page() line to this:
> $page->PrintHeader(“Welcome!”);
But actually you replace the line "$page = new Page(); " in index.php with this:
$page = new Page("Welcome!");
As you do in the support files I downloaded.
Quite confusing for a beginner.
Thanks anyway.
Michael Grossblat / 10/12/2009 / 10:41
The issue with object oriented programming is its learning curve. Simply, you cannot expect one to learn such paradigm of software construction just by looking at simple code, which reason why a web designer must follow the path of the unknown. To learn how to be an achieved OOP, one must learn first OOP language, object oriented analysis and design plus design patterns. This is not all also, potential OOP must develop a minimum of 5 projects. Moreover, he/she must develop UML skills later and be sure to pickup a design method such as iterative, extreme, event driven or even compromise amonth severals.
Regards
Michael Grossblat







