OOP does not equal portable or shareable

So, just now, I was reading a good Rails post by Stuart Herbert and nodding my head along.  I have not gotten into the Rails bashing fun on my blog, but I do poke fun at it around the office.  Then I got to this part:

The OO in Rails continues to leave PHP for dead, and OO brings many advantages to a thriving development community.  There are real advantages to being able to share code between both the must-be-real-time web front-end and the non-real time backends, and to be able to easily reuse whatever external open-source libraries save you time and effort.

Now, I have no idea about the first part.  I am not an OOP guy.  But, what I have issue with is the idea that for code to be reusable, it has to be OOP.  So, if I am a college kid or young PHP developer, I would read this and think “Oh, so, to reuse code or share it, I have to be using OOP”.  Man, this is just so dead wrong and irresponsible.  Can someone tell me why only OOP can be reused?  Why can’t people write sane functions that can be reused?  I do it every day.  They do it in C all the time.  Our front end web servers run the same code base as the cron jobs that do a wide variety of things.  They use the same libraries.  They use the same objects (yeah, i use them when they are a good idea).

Please, someone explain this to me.

(I have a half written post about how you can write good, maintainable, reusable code without OOP.  I have not finished it yet, but I guess I need to.  It seems the world is going to OOP hell otherwise.)


17 Responses to OOP does not equal portable or shareable

  1. Can’t wait for your article. I left procedural behind a long time ago and my mind has been set on OOP as the best approach to writing scalable and maintainable code. However, I’d really like to see a point of view like this — I anticipate that I’ll learning something I should have known a long time ago.

    It’s hard for me to see it though… what OOP really does is group a bunch of functions together and allow them to share a scope by utilizing object properties and the like. That coupled with class inheritance just makes things so much more readable for me.

    Perhaps people prefer OOP because it’s the closest way to write code that resembles the way our minds group things?

  2. Brian Moon says:

    Hmm, maybe my mind works differently then. You touch on one thing that OOP does do in PHP better than procedural – scoping. That is when I use an object. Things like generating a standard report, database interaction, etc. Those need scope and state. But, strings don’t need scope. I have seen too many objects that are nothing more than a constructor.

  3. I agree with you completely. I prefer C to C++ and procedural code to object oriented code. I have thousands of lines of procedural library code that I use over and over.

  4. gasper_k says:


    I agree with you here, procedural code can and is reusable. Even more, OOP isn’t inherently reusable, it has to be written in such way, just as non-OOP. Reusability is also overrated, it comes well behind clarity and maintainability.

    As for your post about reusable non-OOP code, I think it’s good as a proof-of-concept, but I’m afraid many will understand it wrong. There are likely people that will take it as yet another “proof” that OOP isn’t necessary. Besides, I actually have an opinion that OOP will get you further and people should learn, use and prefer it as soon as possible, especially in high-level programming or scripting languages. PHP is actually the only one that doesn’t come close to “everything is an object” notion, and this is (imo) one of it’s biggest drawbacks.

  5. Hi Brian,

    Glad you liked most of the article. The point you’ve raised sounds like a re-run of my debate with Ilia during the 2004 php|cruise conference :)

    The one thing good OOP brings over procedural code is encapsulation, the ability to safely embed code without having to worry over global variable clashes and the like. An object can encapsulate both state and behaviour, whereas procedural code ultimately has to share state. Once you start to mix in enough code from third parties, you can and do get clashes in that shared state.

    C++ has always been a bad advert for OOP programming, but it’s not the OOP-ness that most people hate about it, it’s the attempts to add meta-programming coupled with the complete lack of assistance around reference counting and memory management. Dynamic languages such as Python, PHP and Ruby don’t repeat C++’s mistakes.

    As a developer, and as someone who manages teams of developers, being able to re-use suitably-licensed code from the ‘net, gives my team a competitive edge. We can do more in less time, and we can do it better at the same time (if we can find good enough code out there :) I always see more code re-use in projects that use Perl or Ruby, and I always see much more re-inventing the wheel in projects that use PHP. This is down to the benefits of encapsulation of objects from CPAN and RubyForge.

    I was an open-source C programmer in the 90’s. I know that you *can* write large pieces of code in C. You can make the code maintainable. You can share the code easily amongst your own projects. Good C libraries will prefix all their function names, and all of their structs, and will insist that you pass all data in and out. But all that requires a discipline that must be strictly followed. It’s fine for <1000 libraries or so that make up your average Linux system, but that’s a small number, and all these libraries know what their target is (to fit into the Gnome environment, or the KDE environment, or to fit alongside headline libraries such as libxml). But they all run in the same namespace, and they all get linked in at runtime into the same shared symbol tables. One of the reasons you can’t run mod_php v4 and mod_php v5 in the same copy of Apache at the same time (without getting the compiler to cheat for you!) is because both versions of mod_php export large numbers of symbols with the same name. That’s a real example where you can’t share code written and published by the same authors, directly because they used a procedural style.

    Should everything be OOP? No. One of the reasons I like PHP is because I can choose. I can create objects where I believe it makes sense, and I can write functions where I think it makes sense. But if you really want to share code on a scale far beyond what the PHP community currently achieves, OOP makes that a lot easier.

    Best regards,

  6. Coreflaps says:

    I agree with you – there are many examples of perfectly reusable procedural code. As mentioned in other comments, OO is useful in making scope and state more manageable, which is not always essential – but can be. Furthermore, object orientation can facilitate extensibility, which is an interesting form of re-use. Lastly, I personally find that good object orientated structure applied to a large library can provide an element of self-documentation that is difficult to achieve with procedural code. Smaller libs, on the other hand, may well be easier to understand in procedural form. :)

  7. greyscope says:

    This is nonsense oop is completely reusable oh yeah and don’t forget to buy cialis from me thanks. peace

  8. Nelson LaQeut says:

    Your argument makes sense, but really it boils down to: if you write bad code, then it does not matter if you use OOP or not because the end result will be badly written code…

    There are many techniques that are seen as ‘best practice’ in the programming community – especially with less experienced developers who think a technique written in a book can make them good programmers overnight. The issue is that OOP/Design Patterns – even these new programming methodologies – do not make you a good programmer, and do not make your code well written. They are good techniques, and all offer some things that have the potential to give you a rough idea about how your code or coding process should be structured, but you can misapply anything; from weightless to programming.

    Being a good developer over a bad one is how well you can make code work, in less time developing, faster (performance) and (if the specification requires) extendable. You have to play around with all of these things, from procedural code to OOP code to all the design patterns you can discover – until you find the best match for a particular situation.

    You cannot simply throw out procedural code (like these OOP fanatics), but you cannot throw out OOP code either (like what your post does).

    It’s all about the purpose of what you code should accomplish, that determines it’s structure. There is no “Master” pattern or “Master” library you can develop, because each situation requires careful thought, trial and error and knowledge of past experiences.

  9. Brian Moon says:

    @Nelson: Are you agreeing with me or not? I can’t tell. I think my whole point was that OOP does not make code automatically better. I don’t really think I threw out OOP. I openly admitted that I use objects in my coding. Perhaps you should read the post again.

  10. php code says:

    […] I do poke fun at it around the office.? Then I got to this part: The OO in Rails continues to leahttp://brian.moonspot.net/2008/06/06/oop-does-not-equal-portable-or-shareable/Taco HTML Edit 2 Adds Code Completion, More The Mac Observer Taco Software announced the immediate […]

  11. stef13013 says:

    You’re quite right. OOP will not save the world… at all.

    But today if you don’t do OOP you are a looser !

    It’s just like people who are using XML for doing a simple .ini file :)

    Repeat after me : If you do simple thing you

    You MUST use OOP everywhere, anywhere, at any price.
    Don’t think, do it


  12. Ok, I think everyone here can agree that:

    1) You cannot write an entire software package and keep it maintainable and clean in PHP and use nothing but OOP. You will need to mix it up no matter what you do.

    2) OOP is not the solution to bad programmer’s mistakes.

    I personally have a library of functions that I reuse inside of my framework. These include useful functions to handle strings, debugging, simple data validation, etc.

    Any time I need to write some code that has multiple steps that need to be reused throughout, I will use a class. The notable exception here is functions that recursively process some data.

  13. Sebs says:

    Just writing

    class {


    isnt oop ;). No matter what useless discussion takes place above: its different and the last 10 years should be enough time for everybody to learn about oop and …. stop … this is a php discussion .. yeah ;)

    The solution is: THIS IS intended behaviour, we already know everything, the others are wrong AND

    THE CAKE IS A LIE !!!!

  14. stef13013 says:

    In fact OOP is a state of mind not a language !

    You can program in OOP even if you do it in C !!

  15. zy says:

    well. i’ve been programming for eight years now.. started in C..
    coding in C in procedural and OOP.
    i’m now coding PHP in the OOP paradigm though, i don’t implement the method overloading, and others. sometimes when i need something quick solution for a problem and it wouldn’t be used in other features, i just write a quick procedural function instead coding in a class.
    so i can say even if you code procedural, you can have the same libraries in OOP. but the problem is scoping the variables within them.
    i disagree with you on the topic “OOP does not equal portable or shareable”
    even procedural codes can’t be portable or shareable.
    there are solutions for each problems, and everyone has their own way of dealing with it. with OOP or procedural. its a paradigm.

  16. Brian Moon says:

    @zy I don’t think you disagree with me. Perhaps I am not clear in my post. I do not claim that procedural means portable either. My point is that some people believe that to be portable, the code must be OOP.

  17. The guy made the biggest slip of the tongue that anybody has ever made.

    There’s no way that Object Oriented PHP is any more or less reusable than “old” php. The most sweeping change with objects is that they change the rules about scope.

    I am today constantly reminding myself “Oh, put that in a class” and “Oh, make it inherit from this” and “Oh I’d better make this function final”. Why? If you store an object in a session, or you lose the file that created it, then it _breaks_ and you’re in fact _worse_ off that what you would have been if you had used php’s powerful array and function types. If you’re going to implement a get_var_x() method and a set_var_x() method, you may as well just make it public, right? And yet this is what so many of the classes I’m seeing are doing. At least with the procedural code I can just drop in a file, fire it up, and start working – and access my functions wherever I like, any time.

    Yes, I’m not an entirely Object-Oriented Programmer. I’m more Component Oriented. Usually objects are the best for this, I admit. I love object-oriented programming, it changed the way I look at my code radically and allows me to write far neater and more beautiful works.

    As another commenter pointed out, you can’t just have Object Oriented Programming without a procedure. That’s like having Actors and no script, or a film reel without a projector. Frankly I’ve tried Rails, and in some ways I found it to be great, but compared to PHP5’s Object goodness, I saw no compulsion to learn. If anything it was almost wanting.
    I mean Rails is great, some jobs are for PHP and some are for Rails…

    But to say that Rails leaves PHP behind… This guy obviously hasn’t written any PHP in the last 5 years.
    … To say that Object Oriented Programming is a requisite for reusable or useful code is just about the biggest mistake you can make.

    The part of his brain that sees a pile of dishes was awake, but the part that washes them was asleep.

    So Why do we write so much OOP code?
    – Because we can and it helps
    – because people will laugh at us if we don’t
    – just to fit in and try and look cool, try to seem “with it”

    And as pointed out above, “class foo { }” by itself just doesn’t do anything interesting.

%d bloggers like this: