Feed Sign in with OpenID OpenID

Simon Willison’s Weblog

PHP5 and Questioning OOP

An Interview with Sterling Hughes on PHP5 from the PHP-Con site:

Personally, while I have programmed with both C++ and Java, I am mostly a procedural guy. I feel that most object oriented programming consists of abstracting different problems into the same problem and then resolving that problem. Sure it makes it easier, but its incredibly inefficient for developing a small set of interconnected programs, which really is the whole point in web development.

That’s not to say I’m unhappy with PHP5. Most of the stuff in PHP5 is great, most notably exceptions; I’ll certainly be taking advantage of many of the new features in Zend Engine 2. But most of my code is procedural, and Zend Engine 2 is mostly an object oriented functionality upgrade.

That’s the second time this week I’ve come across an opinion that questions the benefits of the OOP-at-all-costs approach to programming. The first was in Paul Graham’s The Hundred Year Language:

Somehow the idea of reusability got attached to object-oriented programming in the 1980s, and no amount of evidence to the contrary seems to be able to shake it free. But although some object-oriented software is reusable, what makes it reusable is its bottom-upness, not its object-orientedness. Consider libraries: they’re reusable because they’re language, whether they’re written in an object-oriented style or not.

I don’t predict the demise of object-oriented programming, by the way. Though I don’t think it has much to offer good programmers, except in certain specialized domains, it is irresistible to large organizations. Object-oriented programming offers a sustainable way to write spaghetti code. It lets you accrete programs as a series of patches. Large organizations always tend to develop software this way, and I expect this to be as true in a hundred years as it is today.

As undergraduates, we have been exposed to the joys of object oriented programming from our first week at Uni, possible because we’re supposedly destined for the large organisations Paul mentions. It’s nice to see the view from the other side of the fence for a change.

This is PHP5 and Questioning OOP by Simon Willison, posted on 11th April 2003.

View blog reactions

Next: Lots of RSS Aggregators

Previous: Verbose Regular Expressions

9 comments

  1. Yeah, I heartily agree to the 'oo-at-all-cost' being a big problem. One big benefit as to reusability in pure C-libs for me is the fact that they are basically a set of reuseable functions. No need to learn a complete set of classes and their interfaces, just the functions you need and what parameters they expect. Functional programming has for me a far greater merit at reuseability than OOP... But then, I'm just a self-taught scripter/coder ;)

    Martin - 11th April 2003 12:35 - #

  2. Martin, when you say functional, do you actually mean functional, or do you mean procedural like the C libraries you talk about? Functional programming is a distinct concept which some people confuse with programs based around a bunch of (e.g. C) functions.

    Nicholas Wolverson - 11th April 2003 16:58 - #

  3. Having to worry about seeing the other side of the fence isn't a problem when your Dad is one of oo's biggest critics ever. Still, I suppose not everyone has that benefit. As it is, I can see that oo has advantages sometimes, but then again, you look at, say, Java's static methods, which are, when all is said and done, basically a hack to let you do procedural stuff, and you wonder why they don't just let you do procedural when you need it.

    Lach - 12th April 2003 01:53 - #

  4. I have to say that, having worked with a very talented Java programmer and who has implemented our web application framework using PHP4 OO I can see the *real* benefits in non-noun type situations. It really is a problem with the way oo is taught in that inheritance becomes the 'way' to do things. After reading a great book called 'Java Design' by Peter Coad and Mark Mayfield I realised this problem and the associated answer being the use of composition as a model instead. There a very few situations where inheritance is a true model and people forcing inheritance in the wrong situation is the cause of many problems with OO code. This isn't OO's fault, it's the programmers. Bad programmers can do a lot worse in OO than procedural.. This is from someone who is probably not *there* yet with OO but hopes to be soon. ps My colleague *hates* php, particularly it's lack of persistence, arrays and oo problems.. ZE2 should help with some of this.

    Tim Parkin - 12th April 2003 13:23 - #

  5. Its lack of arrays? Did I miss something? I always thought PHP arrays were better than Java ones, since you can use something more expressive than numbers as the array keys...

    Lach - 13th April 2003 04:51 - #

  6. sorry, my badly written sentence. it's not the lack of arrays, it's the implentation. They have an order that isn't really persistent, they're not sure if they're hash maps, stacks or arrays. The Zend Engine handles arrays as numeric hash maps. So an array can have a numeric index and a text index and you have to remember that they also have an 'internal' index. Oh.. and that internal index can be scrambled using some operators. They just aren't consistent. Personally I would have liked to see arrays as arrays, hash maps as hash maps, etc.

    Tim Parkin - 13th April 2003 22:06 - #

  7. Object oriented programming really is a matter of personal taste, if you ask me; however, I find it much easier to reuse code and maintain old code that has been constructed in an object oriented manner.

    The idea of "reusing" code does not simply mean accessing the same tired library from different applications, but it also includes what is done when you extend a class to provide additional, modified, or specific functionality over its parent class.

    If I define an abstract "Database" class, I can build a framework around this generic "Database" class without even ever having a real, implemented database subclass. Others can then create their own specific implementations for whatever database they care about and it will work in my framework. Additionally, if one was to distinguish that all forms of databases to be implemented would fall into either the "server" database or "file" database categories, many of the more specific operations could be outlined in advanced as well, such as opening a connection, or opening a file.

    try
    {
    $DB = new MySQLDatabase("localhost","root","");
    $siteTitle = $DB->Query("Select SiteTitle From Options")->GetNext()['SiteTitle'];
    catch(DatabaseException $e)
    {
    # ...
    }

    A clear benefit is that this structure will not only catch exceptions of type DatabaseException, but all subclasses of that exception type, while still allowing for the developer to catch a more specific exception type first.

    Going one step further with the "framework" idea I talked about. Lets imagine you are helping to develop the newest and coolest web based forum system in PHP and your team is hoping to support some sort of database abstraction system. Using a system such as this, you can code your functions without having every target database library accessible to you:

    function InstallForum(Database $DB)
    {
    $DB->Query("Create Table ...");
    }

    # ...

    function GetThread(Database $DB, $id)
    {
    $DB->Query("Select * ...");
    }

    Of course, this introduces concerns of SQL compatability, but keep in mind that these are just examples. I personally would like to see the procedural implementations of such ideas.

    OO programming compartmentalizes your code, so you can have 20 different "Close()" functions all doing various things, but since they are differentiated by being defined within separate classes, all you need to remember is one function name. $Socket->Close(); $File->Close(); $Door->Close(); as opposed to socket_close($socket); fclose($file); door_close($door);

    One final point I would like to highlight is the general, plain english readability of OO code. It can usually be read fluidly in English. $Dog->Bark(); as opposed to bark($dog);

    Andrew Moyer - 7th January 2004 23:09 - #

  8. OOP can be seen as a way of solving (programming) problems. Problems are solved by designing objects that interact with each other. You can do OOP in C if you want.

    for ex:



    obj_car = new_car(); car_set_color(&obj_car, COLOR_RED);



    Having said that, Java offers *far* more OO built-in supports than C, but it CAN be done in C!

    Please don't get mixed up between implementation and concept. I don't like Java implementation, but I think the concept is fantastic. Between Java and C, I will definetely choose Java as my programming language because of its great OO support.

    This is true for php4 and php5 too, I'm definetely in favour of php5 because of its built-in OO features. You can do it in php4, but php5 offers much better OO support.

    sid - 26th April 2005 00:19 - #

  9. Oh, I wish people would still bother learning the pleasures of Smalltalk!

    William - 21st July 2006 21:01 - #

Comments are closed.

Previously hosted at http://simon.incutio.com/archive/2003/04/11/moreOnPHP5andQuestioningOOP

A django site