A few years ago I was in New York City visiting one of the many world class advertising agencies in town. This particular agency was an award-winning media company handling the online advertising of some of the best-known brands in the world. I’d been invited to meet some people and had hoped to set up an internship for our students. At the time I had recently published a book on OOP and Design Patterns and the lead developer with whom we met, knew of my work.

I was interested in their process. Online advertising campaigns are updated on an ongoing basis, and if it’s not new and fresh, potential customers are likely to click or tap to a different site. They explained how they used a template, and most of what a lot of “developers” did was nothing more than data entry. (Not quite as glamorous as imagined.) As I asked about their process and software development work, the lead developer suddenly said,

We don’t do it correctly.

At the time I had no idea what he was talking about and let it drop. Next, he said, “Most of the time we can’t reuse the same code.” Then it dawned on me. Design patterns are known for reusable code and object oriented programming (OOP) is all about application maintenance and capacity for change. What he was saying was that they hacked through the code to get the job done. Using a template, they could cobble together a workable solution to get the project out the door on time. This is not to say they created junk as far as the end results were concerned. The software worked as expected and looked great—thanks mainly to graphic designers and world-class digital photography. They were right about one thing; hacked code is very hard to re-use.

The Guilt of the Gifted Programmer

At one time or another, just about every decent programmer I know has felt a twinge of guilt either about

not learning OOP and/or

not using the OOP he/she knows.

As far as learning OOP is concerned, it’s not that difficult, and if most programmers had started with OOP instead of sequential programming, they would think that OOP programming is the natural way of programming. However, most of us got started writing a little bit of code in a sequence, then we learned procedures (functions) and then procedural programming. Then by learning more coding statements, functions, operations and the like, we got better at it and were able to do more things. Most of the PHP books on programming didn’t do much to help even though some of the authors themselves were OOP programmers.

However, we knew (we all knew!) that there is a different level of programming used by professional programmers; including PHP programmers. After all, ever since PHP 5 we’ve had classes, interfaces and a bunch of other OOP tools built into PHP.

Whether you think you can or you think you can’t, you’re right.

So what’s the hold-up? There seems to be three excuses reasons for putting off this jump of faith:

Too busy getting PHP projects finished to take time to learn OOP

Too difficult and advanced (or no self-confidence)

Impractical: Ok for academics but not real world development

Let’s address these reasons one at at time:No Time: The idea that you have to stop the world to learn OOP is wrong. You don’t have to take a hunk of time out of your life, learn OOP, and then go back to programming. You can learn a little at a time and employ it where your can. If you know PHP programming, you can slip in a little OOP here and there. Before you know it, you’ll be writing OOP programs. A good start is with a PHP club like Boston PHP that has a self study group learning advanced PHP and OOP. The group is going through Larry Ullman’s advanced PHP book, a chapter per week. You can go at a faster or slower pace. It doesn’t matter.

OOP is Too Difficult: Most people think that server-side programming like PHP is too difficult to learn. Yet here you are, cranking out PHP programs, creating MySQL databases and doing things no client-side programmer can do. If you take OOP one step at a time, it’s not too difficult. What may be difficult is an OOP way of thinking; almost an OOP Zen state. (I’ll get to that further on.) However, starting and working on practical tasks with OOP is not a genius level activity.

OOP is Impractical Ivory Tower Programming: While colleges and universities teach OOP and design patterns, OOP origins began on both business and academic grounds. Simula 67’s (an early OOP language) primary purpose was to improve the movement of cargo ships through ports. Later, in the 1990s, design patterns developed out of cooperation between companies like IBM and certain academic institutions. OOP and design patterns have always been practical solutions to programming.

The Zen of OOP Programming

The other day I was looking at a simple content management system (CMS) written in PHP. The developer put the whole thing into a single class and let it loose on the PHP community. At first I was appalled, but upon reflection, I figured at least the guy got started using classes. Placing code into a class or interface and using class methods is not OOP programming, but using classes can nudge a developer in the direction of beginning to think in terms of an OOP programmer. In differentiating procedural programming from OOP, Daivd Chelimsky put it most succinctly and accurately:

In procedural programming, a process is expressed in one place in which a series of instructions are coded in order. Whereas in OO, a process is expressed as a succession of messages across objects. One object sends a message to another which does part of the process and then sends a new message off to another object, which handles part of the process, etc. You modify a process by reorganizing the succession of messages rather than changing a procedure. (Single Responsibility Applied To Methods. http://tinyurl.com/9256gey)

By thinking in terms of self-operating modules that communicate with one another, the questions you ask begin to change. You’re not asking about flow of commands (as with a flow chart), but you ask, “Who do I need to talk with to accomplish this task?”

Once you start thinking OOP, you’ll change the way you program, and in the long run, you’ll find it a lot easier to create programs, use modules, and to make changes in very complex systems. You tweak the module, not a spider web of sequences and procedures.

I’d be very interested in any ideas, experiences and tips you have for work with OOP and PHP! Leave a comment, a thought, or an idea.

12 Responses to “No Time For OOP or Design Patterns”

I would add another thought, though. There are times when I think OOP is truly indeed overkill. Those are very few, however. One I think of is a shared set of HTML content between various pages. Using an include statement, that content (say a header…) is brought in and applied to the final output. There would be no reason to create an object based process to do that simple bit of code.

Where does one draw the line? I can already hear it: “well this only connects to the database one time, so I don’t need an object.” False. 🙂 Here’s my general rule: If the action is not testable and there is no logical statements, you may not need to have an OOP approach. Connecting to a database? It is testable (did it connect, did I get the right data, etc…). Building a menu system that marks the current page active? Logical statements. (If built correctly, any other project can just swap in their pages/titles, and the main code works just fine.)

Like I said, times to not use OOP are few and far between, but I would be remiss not to mention them – especially in the context of PHP – of which the first goal was to make personal home pages, simpler, and easier. 🙂

I used a quote from Dwight D. Eisenhower in the Strategy chapter (I think…) that went like this,

“In preparing for battle I have always found that plans are useless, but planning is indispensable.” ― Dwight D. Eisenhower

OOP is a state of mind as much as it is writing code or having the structural elements in the code (e.g., class, interface) to produce “real” OOP. Take another peek at that quote by Daivd Chelimsky. OOP is not so much about whether you can or cannot (or should or should not) write code in an OOP fashion as it is how you approach a programming problem. It’s thinking OOP that makes the difference. Every OOP language including C++, Java and C#, have lots of situations where OOP coding might look a bit over the top. However, that’s not how professional programmers think or should think. Take for example the following:

Most would say that the second way of writing “Hello World” is way more than what is necessary. To me, it’s simply an OOP way of doing something. It takes more code, but so what? The variables and methods are encapsulated and yet there’s an open window (public) where a client can access the code.

You’re right that PHP has humble origins to make simple server-side additions to home pages. However, PHP has taken on a life of it’s own and is now an OOP language. (Where we begin and where we end up are often surprising and unrelated to plans.)

For me, OOP is not a more turgid and stilted way of programming or even more difficult. It is a celebration of a way of thinking.

Thanks for your thoughts and I’d love to hear more from you. Cheers, Bill

We’ve had some spammers generating problems and that may be the issue. Also, there’re are a lot of graphics on the site, and that may add to the slow load. We’d like to hear from you periodically to update your experiences.

I’m a very amateur PHP programmer who programs mostly for fun and I have struggled for years with OOP. I never could get my head around the concept. What finally did it for me was your paragraph;

“By thinking in terms of self-operating modules that communicate with one another, the questions you ask begin to change. You’re not asking about flow of commands (as with a flow chart), but you ask, “Who do I need to talk with to accomplish this task?” ”

I now think of OOP as a ‘way of thinking about programming’ rather than a ‘way to program’ and this works for me.

Thanks for the nice thoughts. OOP is as much a way of thinking about programming as it is actually programming. For me, it was a lot of fun; like when you first start programming and one of your programs works for the first time!

It took me a while to get going, but once started, that’s all I’ve been doing. You might also be interested in, This short Post in the same genre. That’s what took me so long to get out of a sequential rut.

For some reason, more people seem to have read “No Time for OOP” more than most of the others. Always a delight.

I come from a C# background (desktop not web), and what spurred me on to look for a better way at designing applications was from a HUGE frustration of updating my programs. I once wrote a small desktop program of about 2,000 lines and because many elements changed frequently updating the program was a ball-ache so much so I decided not to bother. Until the frustration built up so much that I decided to research OOP, to which I came across a design pattern and for at least one changing part of my code instantly realised it was a perfect fit. So little by little, I’d refactor said program using proper OO principles and designs patterns where needed. I think until you get that frustrated at maintaining/updating your programs you won’t truly appreciate the value OOP & design patterns have. A few days ago I decided to switch with PHP, the first thing I looked at was how to do it OO-style.

I’ve done some work with C# (quite a bit actually), but it was primarily with ASP.NET–sort of the opposite side of the track of what you have done with C#. To be honest, I like C# and PHP, but I like PHP better because it is open source and I can develop and run on different platforms–Linux (including Raspberry Pi), Mac OS and Windows OS. C# is MS-Windows only.

I think that a C# background is very useful for approaching PHP OOP, especially if you worked at all in the Visual Studio context. From my work with C# and ASP.NET, all of the defaults with VS included classes, methods and objects; so OOP-wise, C# very much embraces the concept. The two languages are different in two key areas; primarily in typing and namespace use. Strict typing and OOP design patterns go hand-in-hand, but you can enforce typing (to some extent) by using type hinting in PHP. As for namespaces, C# always seemed to have millions of them and each one has another million classes. PHP has namespaces and built-in classes, interfaces and methods, but they are far fewer than C#. (PHP is more like free range chickens and C# is more like those locked up in giant libraries….) Anyway, I find myself actually learning more about problem-solving with PHP because I generally assume that I’ll have to write the solution myself rather than rely on a built-in version of a class or interface.

In any event, I think you’ll find the OOP advantages in both languages to be the same and your life a lot easier.

Thanks for the response Bill. It was insightful. I felt very much at home when I researched type hinting – I like the feature a lot. I’m quite enjoying PHP so far. Granted, I haven’t coded a proper project with it as of yet, as I’m still building my foundations with it. I have played with ASP.NET (MVC) a little bit, and I found the separation of concerns much nicer. Since when I used C# for desktop, using Winforms I’d use the MVP pattern and using WPF I’d use MVVM. So, eventually I’ll be looking into some PHP MVC frameworks – Once I’ve coded some little projects doing it the “nitty gritty” way first. There is one thing that catches me out, and that’s using the -> notation instead of the dot notation when referring to an objects’ methods & properties – no doubt I’ll get used to that ha. I’m enjoying reading your website. Take care.

I program some in C# and a few years back I wrote a book on ASP.NET and C#. There’s a lot to like about ASP.NET/C#, and if I got stuck in a job programming in that combination, I wouldn’t mind at all except that it lives exclusively in a MS Environment. I’ve been doing a lot with PHP on both a Mac and Raspberry Pi, and while I still program some on my PC w/ Visual Studio and the ASP.NET/C# combo, I like the open source environment of LAMP and the flexibility. I didn’t think I’d ever like PHP as much as strictly typed languages, but with type hinting PHP can do most of what I want.

A few years ago I met Ralph Johnson (one of the Gang of Four), at a conference and we had a long chat about multi-core programming and I did a little of that with C# running pretty much in the terminal. Now I’ve discovered Haskel and hope to learn more about it. However, no matter what language I work with, I always seem to drift back to PHP. I’d really like to see PHP put together a multi-threaded version someday.