Object Oriented PHP Design

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Object Oriented PHP Design

Post by pinehead18 »

It's been a few years since I've hard core coded any large php projects. I've come from PHP 4 procedure style of thinking. I'm about to start a decent size project using php and have been doing a lot of reading about OOP. So my questions i in what scenario's or instances would I rather code in object/class design rather than basic procedural. I've read that the best time to use oop is when you want to make one piece of code that you have to use over and over within your project rather than repeating the same code over again. Would anybody care to explain an example of this? Maybe a MySQL Example?

Thanks in advance for helping me get my foot back in the door with php.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Object Oriented PHP Design

Post by Luke »

I am just going to be honest. If this is your first experience with OOP and this project has a deadline, I wouldn't get started down the OOP path for this specific project. Once you get started with OOP you won't be satisfied until everything in the app is perfect, and since it never will be, you'll never get the project done. I'd recommend learning OOP on a project without a deadline.

Having said that, yes, OOP is great for code reuse as well as maintenance. I wouldn't worry about making your entire project OOP even if you have all the time in the world. At least not yet. Build the project just as you normally would, then refactor piece by piece. This gives you the best of both worlds. Your project gets done well, and then it gets done exceptionally well. I hope this advice helps somewhat. I've just been down that path before and I never finished the project haha.
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Re: Object Oriented PHP Design

Post by pinehead18 »

Thanks for the input. I was thinking of taking the best of both worlds. I know when i code PHP before in procedural programming that I had a lot of repeat tasks. Would it be a good start to just code those tasks as objects and then finish with procedure then go back and modify existing code? I don't' exactly have a strict deadline.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Object Oriented PHP Design

Post by alex.barylski »

There are no instances where procedural is better than OOP unless you are technically required to do so like implementing an API for a library or writing systems code (where speed is mission critical).

The argument over whether OOP is slower than procedural is moot in 99% of cases.

A properly designed OO application will more than compensate for it's overhead when dereferencing the 'this' pointer -- which is what most people are complaining about when arguing the speed of OOP over procedrual. As for additional checks like interfaces and object types, they are programming tools, take advantage of them when you can as the milliseconds lost in processing overhead are easier to swallow than the hours it will take you to track down silly bugs.
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: Object Oriented PHP Design

Post by deejay »

i had the same dilema a year ago and ended up going down the procedural route to meet a deadline, but the project had grown significantly since and now making changes are time consuming. I am now using codeigniter on another project and wish I had used this a year ago. Investment in time now will pay off in the future IMHO.
gregor171
Forum Newbie
Posts: 22
Joined: Thu Apr 16, 2009 5:09 pm
Location: Ljubljana, Slovenia

Re: Object Oriented PHP Design

Post by gregor171 »

I agree with PCSpectra. Codeigniter might be a good framework, but I have to make some more study. OOP using design patterns is "a kind of magic" and it pays off. Using pointers and app level object caching could add a bit but careful.

I've done a lot of reading about OOP in PHP (I came from .NET, so it was logical step) and I strongly recommend book:
Appres (2008) - Matt Zandstra - PHP Objects, Patterns, and Practice
Post Reply