Professional PHP

PHP Programming, Web Development, PHP Advocacy and PHP Best Practices.
« PHP as a Deployment Platform
PDO versus MDB2 »

Why is PHP Code Considered Hard to Maintain?

November 9th, 2006

Tobias Schlitt describes Tim Bray’s talk at the International PHP Conference. (PDF slides) Tim compares PHP, Java, and Rails along several dimensions. One of those dimensions is maintainability. Tim ranks PHP as least maintainable, Rails in the middle, and Java as most maintainable.

This is not a surprising ranking. After all, Tim is from Sun, and the maintainability complaint is common in Anti-PHP rants. I’m not trying to suggest that Tim is anti-PHP, far from it, it seems. I’m just using his ranking as a spring board to ask questions.

Chances are that your average Java jockey or C scientist’s first exposure to PHP is to download one of the popular PHP applications. These are usually the product of some open source mega-project with developers of varying degrees of skill. Our engineer-by-day spends a few evenings with the program. The code is not technically outstanding.

How can something like this be so popular he asks? Yet, the software is successful by definition. Nobody downloads unsuccessful open source applications. The technocrat, heavily invested in his own technical prowess, faced with successful yet technically inferior code experiences cognitive dissonance. The only thing to do is to belittle the successful, but surely offensive code. “I could write better code than this,” he says, or “this code sucks,” or “this is unmaintainable.”

It is easy to dismiss these gripes inside the PHP community. After all, those of us using PHP professionally can write maintainable code in PHP. Ask any programmer and they will tell you, “My code is maintainable.” Who writes all of this unmaintainable code, anyway?

Lets take this gripe at face value for a moment. Why is PHP code considered hard to maintain? Is it the language that produces code that is hard to maintain, or is it that the popular ambassadors of the language happen to be programs that are hard to maintain?

Another common PHP sucks complaint is that PHP doesn’t scale. When you are talking about traffic, there are all sorts of counter examples for this. Personally, I’m dying to learn the story behind those .php extensions on YouTube. But, this post is not about requests per second.

Another kind of scalability is team size. I think that when some people complain that PHP doesn’t scale, what they mean is that PHP doesn’t scale to large development teams or large projects. Now we are back to the maintainability issue.

What is it about PHP that makes people think that it is not suitable for larger development teams?

The criticisms of maintainability and scalability generally come from outside the PHP community. But, there is a common complaint from within the PHP community.

It is hard to find a PHP wish list that doesn’t include namespaces. It comes up again and again.

Sometimes users request a feature without explicitly making their true desires and intentions known. They say “I want feature X,” but what they really mean is “solve problem Y.” Good programmers can hear the request for X, but make the jump to solving Y.

When people ask for the namespace feature, the problem they want to solve is integrating code from multiple parties. I wonder if the frequency of this request is a signal of a problem in this department? Perhaps one that requires more than just namespaces to solve? Is the namespace request a proxy for a larger problem?

What is it about PHP that makes it hard to integrate code written by multiple parties, whether they be different developers or different organizations?

Filed Under

  • Open Source, PHP, Software Design

Related Posts

  • goto in PHP
  • Exceptional PHP
  • On the Perils of Inline API Documentation
  • Classpath Considered Harmful
  • PHP Coding Standards
You can leave a response, or trackback from your own site.

37 Responses to “Why is PHP Code Considered Hard to Maintain?”

  1. aaa says:
    11/10/2006 at 12:48 am

    I love programming with PHP, but for me (my) java code is easier to maintain. The reason is simple – java IDEs. They have a much better tools for code refactoring.

  2. Berislav Lopac says:
    11/10/2006 at 1:55 am

    What is it about PHP that makes it hard to integrate code written by multiple parties, whether they be different developers or different organizations?

    Probably the main reason for this is that there is no single, widely accepted standard for code organization in PHP. It is flexible enough that one can write procedural or OO code, or a combination of both; even if you use OOP-only libraries, they will often have different and wildly incompatible approaches. PHP5 introduced __autoload() to help you organize that, but if you use large number of libraries this could easily become the largest function you have.

    Even the standard method of introducing external code has no less than four variants which have different behaviors.

    The flexibility of PHP is at the same time its biggest advantage and its liability. Tim Bray is right that Java is most maintainable, as it has a very rigid syntax structure — so rigid, in fact, that developers had to invent ways to circumvent it, such as interfaces and singletons. This makes it trivial to maintain Java (and C#) code using IDEs with refactoring tools, excellent code completion and other useful features.

    PHP still lacks a proper IDE tool, with Zend environment getting the closest to the ideal, but it still has problems with resources and stability. Personally I don’t have a problem with this lack, as I have learned to delve deep into my code manually, which has made me a better programmer, as I came to understand some concepts and algorithms that I would have difficulty getting if there was an additional abstraction layer present.

  3. Richard Lord says:
    11/10/2006 at 2:03 am

    I have done very little Java programming so I can’t comment by comparison, but as a PHP developer one of the maintainability problems is the multitude of viable solutions to each problem. If I want to grab a file over http, do I use fopen, file_get_contents or cURL (there’s probably more options too). I like to use cURL but other developers make other choices and when you bring our code together the combined result is inconsistent. This inconsistency reduces maintainability.

    As another example, if I want to use a MySQL database, do I use mysql, mysqli, or pdo_mysql?

    I think there comes a point when old code and methods need to be retired. If others want to maintain compatibility with old functions via plug-ins and continue to maintain old, superseeded plug-ins they can. But the PHP developers need to take a more robust stand on not duplicating functionality by retiring poor functionality sooner when it has been superseeded.

    However, even if this is done, there will still be a multitude of different techniques simply because PHP allows for procedural or OOP code (or a mixture of the two). I see similar problems when working with C++ – one programmers highly maintainable code is another’s confusion of stl containers, iterators, functors and generic programming. We’re all different. Maybe Java offers fewer choices?

    The important thing is, if you have comprehensive coding standards indicating style of code and also what techniques to use (oop or procedural, mysqli or pdo, etc, a framework if appropriate) and implement them throughout the team then is your code easy to maintain for people who know those coding standards?

  4. Peter Bowyer says:
    11/10/2006 at 2:33 am

    Having had to integrate existing apps inside other apps (usually something inside of Xaraya, *Nuke, or Zen Cart/OSCommerce) I join the desire for namespaces. But I think the need is because these existing apps are badly designed – there is already one variable $db in the global namespace and the second application you integrate also defines $db, but with a different DBA…

    If everything was inside classes or functions and all were named Productname_XYZ then the problem would be less. It’s an old problem, and probably my least favourite of the lot because writing unstructured code is faster which for small open source projects is generally what the developer wants :)

  5. Laurence Powers says:
    11/10/2006 at 4:07 am

    Wow yes Youtube runs on PHP it seems.

    You can add .php extension to any URL and it works, but not any other extension.

  6. PHPDeveloper.org says:
    11/10/2006 at 6:16 am

    Jeff Moore’s Blog: Why is PHP Code Considered Hard to Maintain?

  7. bones says:
    11/10/2006 at 6:59 am

    Although i agree with Richards post above about the complexities that arise from the multiple ways to do things.
    I feel the major drawback is due to the ease of knocking something up everyone does, and in a commercial enviroment due to managment, programmers are forced to just add on functionality by slapping more functions etc in to add extra functionality to the original thrown together syatems rather than planning them in to work good.
    Rather than a planned approach working in this sort of software development scheme just develops chaos i have coded in php for 4 years but my last job involved an app that had ‘evolved’ over a 5 year time span and in the 6 months i was there could not understand it for the life of me.

  8. Tim says:
    11/10/2006 at 8:46 am

    First I want to say that I love PHP and Java almost equally, but for different reasons. I would like to comment on namespaces, though, because I think that PHP programmers often don’t get why people want them so badly.
    I don’t really want namespaces just for integrating other’s code (although it wouldn’t be a bad idea). Even for PEAR this would be nice. I often want to take a PEAR class (let’s make up one called FooFilter), wrap up the parameters I use most often and create my own class FooFilter. That’s a pretty standard pattern that I can’t do in PHP. I can have My_FooFilter. But that quickly makes for ugly code. Especially if I want to nest things. And it doesn’t scale well in development departments. Because ultimately you have “that guy”. And “that guy” doesn’t want to name his classes properly. Or he’s always too busy and he’s going to do it at the end of the project and it never happens. That’s one place Java is superior. It reigns in “that guy” a lot more and forces him to follow convention.
    Another common place I use namespaces is in factories. For instance I might want do do something like this:

    WidgetFactory.php
    box/Widget.php
    circle/Widget.php
    polygon/Widget.php

    Right now I have to do something like this:
    WidgetFactory.php
    widgets/Box_Widget.php
    widgets/Circle_Widget.php
    widgets/Polygon_Widget.php

    which is fine as long as you don’t start needing lots of support classes for each widget. That’s where the namespaces really make the difference between a directory full of files and a nice hierarchy.

    And the lack of lexical scoping. That’s bad. Again it makes for hard to maintain code. It’s not an issue if you just have a few people who are really good, and can be trained to follow instructions, but again if you have to hire “that guy” for some reason (and every dev department over about 10 people is probably going to end up with “that guy”), you’re going to end up with all sorts of horrid impossible to troubleshoot problems.

    I don’t think either of these are deal breakers, but I think it does balance out with the productivity losses you lose to Java by having to constantly wait to redeploy code to your application server.

  9. Bill Karwin says:
    11/10/2006 at 8:50 am

    One problem is that many people writing PHP code, regardless of PHP 4 style or PHP 5 OO style, still do things like putting HTML markup inside echo statements, into variables that then get echoed, or worst of all, they put HTML fragments into the database alongside business data.

    A consistent separation between data, code, and presentation should make it easier to maintain all three independently of one another.

    Not that this is a failing particular to PHP — you can do this in most languages.

  10. Hans Lellelid says:
    11/10/2006 at 9:46 am

    I believe that namespaces would go a long way to improving current systems of package integration, but I do also think that there’s a bigger picture to the namespaces request. The problem is that PHP apps want to interoperate, and the reason why it’s often tricky is probably at its simplest “lack of standards”. Of course Java has no shortage of — some built into the language (e.g. foundation classes), some in specs, etc. Rails too, is completely convention-driven in a way that doesn’t make sense to compare it to PHP (I guess that’s the heart of the framework-vs-lang comparison problem). Currently PHP projects can use any number of solutions to handle logging, database access, configuration management, etc., etc. There’s rarely an agreed upon tool for the job or built-in language tools for most of these basic application needs.

    I do think the situation is getting better & better in PHP, though. At the engine level PHP5 has introduced a number of built-in classes (e.g. Exceptions, SPL, DateTime) which provide OO standards for error handling, date-time manipulation, iteration, object loading, etc.

    Now there are E_STRICT-compliant foundation libraries like eZ Components (and Zend_Framework) providing a good set of tools that all our apps need. I hope that PEAR too will start requiring E_STRICT packages, fix their namespace issues, etc. It does look hopeful.

    Anf finally, I think one of the greatest recent improvements in the PHP world is the new PEAR installation system which allows anyone to distribute components *with* dependencies on other components. This is going a long way in making it possible for components to work together in PHP.

    I do think namespaces are a critical component to this big picture of inter-operating components in PHP. The PHP community has really grown up over the past few years, driven by these new language features — and now in turn is pushing for new features (like namespaces).

    Hans

  11. Ed says:
    11/10/2006 at 11:20 am

    http://youtube.com/t/jobs#sweng

    I don’t see any of these lines mentioning “PHP”, instead I saw Python. How so?

  12. prim8 says:
    11/10/2006 at 11:34 am

    * Scope is a real issue – novice developers will just dump everything into a global scope and with lack of namespaces (or strict discipline), collisions ensue.
    * Legacy code – PHP4 was pretty ugly, horrible object support, encouraged widespread use of globals (you had to do some ugly hacks to even get a Singleton working). Most ’successful’ applications were developed under PHP4.
    * Legacy Stupid Configuration Defaults – register_globals, magic_quotes, etc. Lots of people developed for these defaults without realizing the mess they were creating.

    That said, PHP5 code used with thought and some good libraries can be quite maintainable.

  13. warpedvisions.org » Blog Archive » Why is PHP Code Considered Hard to Maintain? says:
    11/10/2006 at 2:36 pm

    [...] November 10th, 2006 in Links A good analysis of why people consider PHP difficult to maintain?. My observation is that it isn’t, unless the code is structured poorly (an affliction possible in most languages), or the developer is reliant on features in higher-level IDEs. [...]

  14. Gábor Farkas says:
    11/10/2006 at 4:17 pm

    Re: Tim’s comment.

    Isn’t it that you are trying to work in PHP as in java? :) .

    now, i actually do not know PHP :) (why is this guy commenting then here?!?!?!? :) ), but i know java and python, and i see it many times that java programmers start to use python, but they don’t really want to use it “the python way”, and basically are writing java code with python syntax.

    as i said, i do not know PHP, so it’s quite possible that i’m wrong, but the whole “factory” thing is really, really java like.

  15. Jacques Marneweck says:
    11/11/2006 at 7:23 am

    Ed, since Google brought them out and Google is a python shop it is natural for them to start moving over to using python on youtube, considering the the BDFL of Python is another Google employee.

  16. Java,PHP y Ruby : nueva comparativa | DESARROLLO DE SOFTWARE says:
    11/24/2006 at 10:17 am

    [...] Muchas voces han salido al paso de las declaraciones de Tim. Aquí os dejo una interesante opinión. [...]

  17. Comparing Java, PHP and Ruby « Andrew Whitehouse’s Weblog says:
    12/2/2006 at 1:41 am

    [...] While scanning the list I came across a post yesterday on Professional PHP, which is 3 weeks old but very pertinent given the languages I’m developing with. The post, entitled “Why is PHP considered hard to maintain”, covers Tim Bray’s presentation given at the International PHP Conference in which Tim compares the relative merits of Java, PHP and Ruby. We aren’t just talking about the languages themselves, but also the framework (for example, JSF/Spring/Hibernate, Apache/mod_php, Ruby on Rails) that allows you to build web applications; of course, you have to be careful which criteria you choose to compare but I think Tim’s presentation succeeds. I’ll cover the point about PHP maintainability below. [...]

  18. Kaitnieks says:
    12/6/2006 at 2:26 pm

    Personally for me: If I change a function call and add a new parameter for an example, I can’t just recompile the whole project and easily see all function calls that need to be fixed. I have to take extra steps in php or you can call it hard maintainability.

  19. Son Nguyen says:
    12/17/2006 at 4:10 pm

    Depends on the scale of your project and your developers, PHP codebase can be maintained very effective. But when comparing directly with Java, I have to agree that Java has a more structured and consistent principle (from naming convention to all kinds of syntax).

  20. error414 says:
    12/22/2006 at 2:46 am

    It is a problem of PHP that it started as a non OOP scripting language, languages as python or java got ahead of it. It is fashion of java coders who just wrote program hallo word to get at PHP.

    I did not read a study where PHP would not be twitted for any of its mistakes.

    Compare perhaps Lisp with PHP in maintainability

  21. Buddha443556 says:
    2/21/2007 at 11:15 am

    Personally, I think most problems stem from trying to force a complex solution on PHP. PHP likes simple solutions. The more you try to do in one request the less maintainable and scalable the code.

  22. Sigurd Magnusson says:
    2/23/2007 at 2:13 am

    I think PHP follows in much the same footsteps as HTML, but has somehow got a bad rap. Once upon a time (okay, fine, its not over) there was HTML tag soup but then people decided to do a better job with practically the same tools and options available to them, and you now have a whole webstandards movement. In the same way, the coding conventions (from where you put new lines all the way to how you construct your apps, like using OO and you bloody well better not intermingle your HTML and PHP) have matured. While we all strive for ruby-style elegance in our PHP5 projects, let’s not forget our history and the importance of the frontier days. HTML3/4 and PHP3/4 were VERY useful; the ability for anyone to bash something together is a cornerstone of the mass-developer innovation rampant on the internet.

  23. Jeremy says:
    3/5/2007 at 3:29 pm

    I will second the mixed language problem. I’ve seen and written some horrible PHP code. In my case its usually caused by mixing PHP/HTML and javascript all into the same mess. The problem is that its so easy, and there are so many example on how to do it wrong, that it appears everyone starts off on the wrong foot. Once you gain some experience you soon discover how bad the code really is.

    Its like C/C++, lots of people talk about how great the K&R book is. When you go back and read it after a few years experience, you discover that the examples are good for instruction but absolutely terrible code. 90% of the examples in that book utilizing pointers could be written without the pointers and be just as fast, 10x as readable, and nearly all the resulting bugs/gochas would just disappear. Yet, that doesn’t loads of projects being full of the same tired old crap.

  24. Dan says:
    4/2/2007 at 3:33 am

    PHP needs to be redesigned. Its inconsistencies a PITA.

    var $variable; // is valid/error depending on where you it
    static var $variable; // is an error, correct usage: static $variable
    self::$variable but $this->variable // why not make it self::variable???!!!!

    The method/function name inconsistencies are too numerous to be listed here.

    PHP NEEDS TO BE REDESIGNED!

  25. Aree Cohen says:
    8/15/2007 at 1:37 am

    Hey Dan,

    What seems to be the problem with those examples??

  26. Marc says:
    9/14/2007 at 2:20 pm

    There’s no problem with php as a language. It’s the user that write the “unmaintainable” code.

    And as for scaling, Digg.com one of the net’s biggest high traffic sites uses php as their programming language.

  27. Cody says:
    9/17/2007 at 11:11 am

    PHP is no different to any other language, there are situations where it’s perfect and other situations where it’s not; saying that one language is superior over another is therefore a bit of a none starter. PHP’s beauty is its flexibility, that can also be a shortcoming in situations that it is ill suited.

  28. Visitor338 says:
    9/24/2007 at 12:00 am

    Your site found in Google: http://google.com/search?q=hmp

  29. 小狐狸DM » Blog Archive » which site write in PHP says:
    5/22/2008 at 3:34 am

    [...] at the moment? Written in PHP. YouTube, the most widely known video sharing site on the internet? Written in PHP. Facebook, the current billion-dollar zombie-poking social networking darling of venture [...]

  30. PHP Sucks, But It Doesn’t Matter | Developer Home says:
    5/22/2008 at 3:16 pm

    [...] at the moment? Written in PHP. YouTube, the most widely known video sharing site on the internet? Written in PHP. Facebook, the current billion-dollar zombie-poking social networking darling of venture [...]

  31. La diferencia entre “opinionated” e ignorantes de su ignorancia « Ricardo Galli, de software libre says:
    5/31/2008 at 9:25 am

    [...] at the moment? Written in PHP. YouTube, the most widely known video sharing site on the internet? Written in PHP. Facebook, the current billion-dollar zombie-poking social networking darling of venture [...]

  32. (function() { : Php Pattern says:
    6/7/2008 at 12:05 pm

    [...] at the moment? Written in PHP. YouTube, the most widely known video sharing site on the internet? Written in PHP. Facebook, the current billion-dollar zombie-poking social networking darling of venture [...]

  33. Anonymous says:
    9/9/2008 at 12:56 pm

    I have not run into any unmaintainable code in any computer language until people start putting in idiotic “frameworks” like MVC in languages that aren’t inherent to it or FuseBox in ColdFusion.

    Be it BASIC, ColdFusion, PHP, or Perl.

  34. Tom says:
    12/16/2008 at 3:50 am

    So… you argue that PHP sucks because it doesn’t have namespaces. Well, now it does! Congratulations, you suck!
    Oh, wait… The chosen namespace separator is BACKSLASH?!? And this was decided by a couple of incompetent programmers over an IRC conversation? I guess I should tell you I’m sorry for insulting you and I have to agree with you: PHP sucks!
    I’ve been using PHP for quite a while now and I’ve been waiting to see a lot of improvements. The language has seen some improvements, but not the ones we’ve expected…

  35. kevinxiao says:
    8/3/2010 at 12:07 pm

    I feel the major drawback is due to the ease of knocking something up everyone does, and in a commercial enviroment due to managment

  36. Mecan Steve says:
    8/17/2010 at 3:23 am

    great work !!

  37. jessica says:
    8/25/2010 at 8:42 am

    I guess I should tell you I’m sorry for insulting you

Leave a Reply

Click here to cancel reply.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

code: use [code=php][/code].

Comment Preview

    Subscribe Feed
    Share Subscribe to this blog…
    Share Bookmark or share this page…
  • About

    My name is Jeff Moore. I'm a PHP programmer living in San Francico and working for a startup.

    More about me…

  • Categories (Home)

    • Agile Methods (14)
    • Mac (14)
    • Misc (17)
    • Open Source (14)
    • PHP (98)
    • Software Design (29)
    • Usability (14)
    • Web Design (20)
  • Recent Comments

    • Programming Language Trends via Google  19
      Craigslist pva, jessica, Scott [...]
    • Looking Towards the Cloud  35
      bentonville multiple listing, cosmetic dental, Sam Brodish [...]
    • PHP versus ASP  8
      Marhta Blight, Ravi, Ryan Brooks [...]
    • How to Transfer Mac OS X Application Data between Computers  59
      Website Migration, harry the computer support guy, Dotty Salvage [...]
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)  157
      lehuuphuc, Robert Parthemer, Lingerie Intimate [...]
    • PHP Games  25
      jessica, Tennille Cranor at Chilli Plants, Lucas Ortell [...]
    • un-PEAR-ing  5
      jessica, Eugene Panin, Arnaud [...]
    • The Legality of Republishing RSS Feeds  23
      kevinxiao, Marissa Miscovich, Quick Student Loans [...]
    • Faster Page Loading  4
      jessica, angular cheilitis, Aaron Rosenfeld [...]
    • PDO versus MDB2  15
      jessica, kevinxiao, Gavin [...]
  • Recent Posts

    • ZendCon: Writing Maintainable PHP Code
    • Looking Towards the Cloud
    • Holiday Tech Support
    • Closures are coming to PHP
    • php | tek Wrapup
    • php | tek 2008
    • Sarah Snow Stever
    • Benchmarking PHP’s Magic Methods
    • The Endpoints of the Scale of Stupidity on Video
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)
  • Site

    • Archives
    • Log in
  • Search