Is PHP the new VB6?

Introduction
In the past, I've come down hard on ASP.NET for trying to be an all-in-one solution. Rather, I've come down hard on ASP.NET developers trying to make it into an all-in-one solution. ASP.NET is an unsurpassed language for enterprise development and other medium-to-large scale projects. Despite significant improvements in 2.0, the simpler your project gets the more you begin to notice .NET's overhead (no, I'm not talking about performance).  In fact, everything that makes ASP.NET a great toolset for large-scale development is what makes it such a bad choice for anything small.

On the flip side, PHP excels at this type of small/medium work.  It's extremely easy and quick to put something together and supports an incredible amount of kludging before falling apart. Of course, everything that makes PHP such a great toolset for small-scale development is also what makes it such a horrid choice for any large-scale projects or any sized enterprise development.

I just want to take a little sidetrack to acknowledge that there are in fact tons of large and complex PHP websites out there as well as tons of small ASP.NET sites. In the end, despite everything I'm going to say, the individual developer has far more impact on a project's success and implementation than the tool he or she uses.

PHP is NOT Object Oriented
As a language, PHP's object-support is quite rich. Since the introduction of PHP5, developers have had the same level of OO support as most other languages. Of course, there's nothing forcing developers to use OO principals. This is a mixed blessing. For projects with a small domain, I've found that OO can slow down development without brining any substantial benefits.  For projects with a medium/large domain, developers are left on their own to do the right thing. I've heard it said more than once, but PHP doesn't have a culture of objects. The PHP community hasn't bought into the concept of OO the way the Java, Rails and .NET communities have.  Whether that's an issue with PHP or an issue with PHP developers is hard to say. VB6 had decent OO support; most VB6 developers just didn't buy it (or maybe they didn't grok it).

As a framework, PHP fails to leverage OO in any meaningful way. The fact that PHP itself isn't OO explains why developers don't use PHP's OO features. In .NET everything's an object. Want a textbox? Declaratively create a new textbox objects, programmatically set properties and execute member functions. Most PHP, VB6 or classic ASP programmers just don't get it. Think of it this way, in .NET you create a server-side DOM, manipulate it with a very rich API and an event model then render it out. This is particular useful when you want to extend the built-in objects and achieve greater re-use (within the same project or with completely separate projects).  There are countless other examples, such as user controls, HttpModules, HttpHandlers and the page lifecycle which are completely foreign concepts to PHP developers.

Beyond the presentation layer, PHP's vast library is also mostly procedural. The contrast between the MySQL and MySQLi libraries is a good example of how PHP is (MySQL) and how it should be (MySQLi). However, almost all of PHP's libraries are procedural: string manipulation, regular expressions, sessions, ftp, sockets, Images, database access (MySQLi is the exception) and so on.

With PHP's own miserable ue of OO, it's not hard to understand why PHP developers don't leverage OO as much as they could. OO is hard, and PHP neither demands it nor does it bother promoting it.

Layering
The only support for layering offered by PHP is the include/require functions.  Like OO, proper layering can greatly increase code readability and maintainability. Much like in classic ASP, most developers tightly intertwine their PHP and HTML. The resulting code is truly worthy of being labeled spaghetti for its incomprehensibility and the difficulty to maintain. "echo" is core to PHP development, while the equivalent Response.Write is scarcely found in ASP.NET. Despite being easily abused, ASP.NET's CodeBehind model and DataBound controls are well ahead of any PHP offering. The lack of OO buy-in often leads to anemic domains.  On the data access front, the .NET community is as far ahead of PHP as Java is ahead of .NET. The introduction of LINQ in the next version of .NET will put it squarely ahead of all if developers adopt it.

The first example of the MySQLi documentation in the PHP manual highlights the problem perfectly:


<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = $mysqli->query($query)) {
   /* fetch associative array */
   while ($row = $result->fetch_assoc()) {
       printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
   }
   /* free result set */
   $result->close();
}
/* close connection */
$mysqli->close();
?>


I can't really think of a worse way to write this. True, this is only a reference document, but most PHP code follows the same pattern.

Exception Handling
Exception handling in PHP is like object oriented programming – it's supported but not used. The PHP framework scarcely makes use of structured exception handling as do most of the online samples.  The example which always gets under my skin is the MySQL library. Check the return value to see if a connection failed and echo out mysql_error() . Anyone who thinks using "or die" is any better just doesn't get it. Even the much improved MySQLi library didn't get it right.

It doesn't really get any simpler than this: a failed database connection should throw an exception and in almost every case developers should let it bubble up to the global handler. I'm sure they exist, but I couldn't find a single standard PHP library which actually uses exceptions.  

There's a base exception class which you can extend, great! But why bother when you'll still have to do that oh-so-dated return value check all the time. PHP developers are quick to point out that PHP 5 has first class exception handling but slow to actually use it.

Other Issues
The fact that PHP is a loosely typed language can lead to significant difficulties for larger projects.  Strongly-typed languages like C#, VB.NET and Java can take full advantages of compile-time checking and design-time tools (IDEs) while loosely-typed ones generally can't. (I have little experience with PHP IDEs, so maybe this isn't as much of a problem as I think it is).  Like everything else we've covered, the lack of strong typing generally makes PHP ideal for smaller projects and problematic for larger ones.

.NET also has a clear edge when it comes to tool support. Visual Studio has long been considered one of the best IDEs around. It does cost a fair chunk of change (we are talking about medium/large apps though), but there's the free Express line which is quite adequate. For very large projects, PHP doesn't have anything like Team System.  When it comes to profiling, debugging and refactoring ASP.NET is ahead by at least a couple years. VS.NET's projects, solutions and references are all integral to code re-use. PHP largely goes by the copy-n-paste approach.

Although both PHP and .NET lag behind Java when it comes to Agile methodologies, there's considerably more awareness and support for Test Driven Development, unit testing and Continuous Integration within the .NET community. .NET's tools are more mature and widely used. Searching for information on TDD, unit testing and CI for PHP returns only a handful of useful/relevant hits.

Conclusion
VB6 and VBA share a lot more in common with PHP than with either VB.NET or C#. More pointedly, VB6 developers share a lot in common with PHP developers.  Despite having some of the necessary tools, PHP developers (including those working on the actual PHP codebase itself) seem unwilling or unable to make use of core software engineering methodologies.

From my own experience, and the countless of online tutorials and blogs, many PHP developers are guilty of the same crap code VB developers were once renowned for. OO, N-Tier, exception handling, domain modeling, refactoring and unit testing are all foreign concepts in the PHP world.

This isn't an anti-PHP rant – it's a using the right tool for the job rant.  There are frameworks out there for PHP which help solve a lot of the inherent issues, but like ASP.NET some of the limitations are built-in (you can't get around C#'s strong-typing and you can't get around PHP's loose-typing). There are MVC  frameworks, O/R mapping frameworks and Agile tools.  Despite those, with the 5 year anniversary of ASP.NET a couple months away, and significant innovation in the next release of .NET, PHP is starting to lag far behind.

This entry was posted in Grab a coffee before reading. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

22 Responses to Is PHP the new VB6?

  1. Sancar Saran says:

    Hey thanks it was good article to read.

    And here my rant

    PHP was web framework of C and C++ (with apache of course).

    In web, every request processed in 1 second and guess what ? Zend going to wrtie full scale OO based framework for framework (read as zend_framework for php) because some Desktop programmers argues the way of PHP.

    Bahhh…

    Problem is not PHP, Problem is there where too many unemployment Desktop programmers around here to trying find web programmers job and when they found a old php code in their new job, they start to crying. Oh s***t this code unmaintainable. Then they create new mission for themselves.

    Lets start to teach proper programming way to those poor php programmers.

    And because of those desktop programmers. Zend start to write a framework.

    Look mate, this is web, things get old in a year. Today I see a messages in Zend_Framework mailing list to refactoring Zend_Framework for version 2.0.

    Oh Yesss…

    So ? if you stick to desktop programming ways, you found yourself the continuous update back end because ways are constantly changes in the web.

    And remember we are here to writing new web pages

    Regards

  2. Brad says:

    Just a followup for 2008, PHP has *slowly* been improving. Zend_Framework (for example) is starting to replace a few of the more procedural libraries.

    PHP is also moving glacially towards supporting strong(er) typing.

    The articles main arguments are still valid, though. PHP can easily be used in a procedural manner and if you don’t have an OO background, best of luck to you.

  3. karl says:

    You’re right Alan, I hadn’t considered the [unfortunate] need for MySQLi (and the rest of the functions) to be backwards compatible for 4.0. I guess the move from ASP –> ASP.NET softened me to these kind of forced/breaking changes, but it’s a very good argument.

    You might be interested in a piece I wrote bitching about ASP.NET :) You might find more balance in my point of view when you put the two articles together:
    http://codebetter.com/blogs/karlseguin/archive/2006/05/12/144584.aspx

  4. Alan says:

    Karl: Good article. I am a die hard PHP programmer but agree with most of what you said. The parts I don’t I think you clearly showed both sides of the argument, thank you for that.

    However, I came away from the article feeling one of you biggest contentions is OO in the PHP libraries especially in your MySQL and MySQLi examples. Let me clarify at bit on why the developers of those chose to write them the way they did instead of using the power of PHP5 OO.

    Those two libraries are both written in PHP and are PHP4 compatible and PHP4 has very little OO functionality. So in writing them the have used PHP4.

    If you would like to see a PHP5 version of the mysql libraries look to PDO (http://www.php.net/pdo). It is a extension written in C for PHP5 that is fully OO PHP library.

    I felt many of your contentions that you gave no argument for on PHP’s side resides from areas of misunderstanding like this.

    Again though, good article,
    Alan

  5. karl says:

    Too much of the .NET community is stuck on DataSets.

    They are a fine tool, but like any tool, need to be used in the right situation. .NET developers tend to use them in all situations.

    The Java community is ahead with it comes to O/R Mappers and other database< -->domain techniques.

    The good news though is that the .NET community is quickly catching up.

  6. Patrick says:

    Good article and i enjoyed it though
    Karl you wrote a statement below
    “On the data access front, the .NET community is as far ahead of PHP as Java is ahead of .NET”
    Can you please elaborate on that and explain why Java is ahead of .Net on the data access front?
    Thanks

  7. karl says:

    AC and Dan:
    You both make valid points.

    I don’t agree with AC’s “language vs Framework” argument though. The .NET framework is nothing more than a bunch of class libraries which let you do a bunch of stuff. They are neatly organized in assemblies and namespaces, but that’s no different than all the PHP functions (except there are probably a lot more in .NET). PHP is a set of libraries, a language, and a web module (for lack of a better word). This, to me, is no different than what people mean when they say ASP.NET. It’s a language (C# or VB.NET), a framework (all the class libraries) and a web module for IIS.

    The fact that ASP.NET is a further ahead than the out-of-the-box PHP offering doesn’t mean you aren’t comparing apples to apples – it just means one apple is a lot shinier than the other. But PHP *IS* a language, it *IS* a set of libraries and it *IS* a web framework.

    That said, I agree that there are a lot of PHP “frameworks” which greatly enhance PHP. I don’t know much (anything?) about them, but I’m readily willing to admit they bring PHP very close to ASP.NET. I have no hard data, but I think their use is limited (I’m happy to accept any corrections).

    And, as I’ve said before, it’s the programmer not the language (or framework).

    The point I agree with the most is Dan’s last one – any popular language (which both really are) is bound to have terrible programmers because most programmers are terrible. But that doesn’t explain why the PHP framework itself is terrible (which I realize is a near impossible pill to swallow). That the PHP framework is terrible though doesn’t help create better programmers.

    I actually think that .NET is creating better programmers – at a snail’s pace.

  8. Dan McKinley says:

    I can put all of my methods in a single class (or to be even more evil, put all my logic in a single method) in any language with “enforced OO.” I don’t think this argument holds any water. And, any insanely popular language is bound to have terrible programmers, because most people are terrible programmers.

    But yeah, PHP sucks.

  9. AC says:

    I think one thing that many, many people get wrong is comparing a language to a framework. How many people can write a large or medium application in VB or C# easily and quickly without using the .NET framework? Not Many.

    There are frameworks available for PHP (CakePHP, CodeIgnitor, Zend Platform, etc) and those make PHP programming much easier for larger projects. They are not as robust as .NET (yet), but they do enforce good programming principles and design patterns (MVC, etc).

    I am not saying that PHP with a framework would be better than .NET, I am just saying that it would be closer to an apples-apples comparison. Also, I do not agree that PHP programmers do not understand programming. Giving an example of the worst coding possible in PHP does not support your argument.

  10. karl says:

    [e]lementar two points.

    First, even if everything you say is true, it doesn’t address the original point, which is, put bluntly: PHP developers don’t know how to program – they don’t understand OO, don’t understand layering, design patterns or simple structured exception handling. Of course it’s a generalization…just like it was for VB developers back in the day.

    As for the cost…I said it about 5 times, but I’ll say it again, I’m talking about medium to large software projects or enterprise development. This is the type of project where the cost of software and tools is less than 5% of the TOC. Your view is incredibly shortsighted.

  11. [e]lementar says:

    hm …come on php is free, fast, flexible and fashinates everybody it’s also easy to understand and build anything on da web . Php code exists everywhere and free spreads the personal education of the future programmer to the level that he gonna simply love it .
    Hence that you like ASP then how you can work with it from da moment that you need to buy everything even the addons cause is not all included there.. php if free and runs on Linux.Wins,solaris etc ASP do not :(
    We well all know that if you like c,c++ then you gonna love php .

  12. kompression says:

    Whether it be object oriented or procedural it wouldn’t matter to me. I can do both. Bascially it should be whatever needs to get the job finished. Sometimes you can/can’t have both. But, whatever. To each his/her own.

  13. Arnold says:

    Hi,

    I’ve worked a lot VBA as well as with PHP. I like to specify two mayor differences.

    1.) VB has a drag and drop IDE where you write code for events. A lot is magic is done in the background. All kinds of objects are automaticly created and the developer has to look into the manual or use the debugger to see which objects you can use.
    PHP starts with a blank sheet. The only variables/objects there are, are those you defined in the sourcecode.

    2.) VBA supports something like OO, but it has no inheritance and therefor you can never create any sort of OO framework. In PHP this works as a charm.

    Differences like these (and a lot more) make the feel of the programming languages are as different as ANSI C and LISP.

    Conclusion: Both PHP and VB are exelent to quickly build small apps. But PHP is also an exelent programming language for large projects. The fact that you see little examples showing the power of PHP5 is that is has yet to be discovered by most.

  14. Barry says:

    I think we’re both arguing for the same thing. I don’t disagree that ASP.NET is much more appropriate for large businesses that require web presence. Just pointing out that SMEs are more likely to go for the cheaper *startup* option which is a LAMP stack, whether or not it is the ideal choice.

    If there’s one benefit of ASP.NET over PHP that I had to pick it’s that the view is fairly well abstracted from the model, which can be particularly difficult in PHP without some discipline.

  15. karl says:

    Geeezz Scott can I steel your wisdom and pass it off as my own plz? :)

    As I’ve said, it’s always more about the developer than the language (and I frequently use the word discipline also), but some “features” are abused so often than at some point the language has to stand up and be accountable.

  16. karl says:

    Ala’a A. Ibrahim, thanks for the feedback. Fregas responded to you better than I could have. Although I don’t think you missed the point, I do think you just happen to be the kind of PHP programmer i WANS’T talking about – and I truly believe you to be in the minority…

    Barry, on the free point…ignoring Mono since I know little about it, I just can’t agree with your line of reasoning – it seems incredibly short sighted. You are absolutely right for a huge number of small-medium sites out there, but at the risk of repeating myself, as your site grows in complexity, or you are dealing with even remotely complex business, the cost of commercial products is only going to be a fraction of the total cost of ownership.

    I know Microsoft people throw around TCO like it was candy, but many studies have pegged code maintenance to be 80-90% of the total costs of software. Even if development tools and platform magically accounted for that other 20-10% (which would mean planning and development would be 0%), you’d still be better off trying to optimize the 80-90% than cut down on the 10-20%.

    I do agree though that it’s still a compelling point in PHP’s favor. Just like the fact that it’s cross-platform. Even though only a very few number of large projects require it..it’s certainly a plus in it’s favor that MS offerings don’t have.

  17. I don’t see anything wrong with the statement. I would imagine that an experienced programmer wouldn’t think of re-purposing a variable just because he could. A less-experienced programmer would require a tool to keep him in line. It really has more to do with discipline than experience, but discipline usually shows up only when it’s got a good reason, and good reasons usually come with experience.

  18. Chris Lively says:

    ——–
    Now, PHP being a loosely typed language, well for me I don’t see it a problem, I like to consider it a feature, an experienced programmer can make a very good use of such feature, but newbies would could fall in it.
    ——–

    Quick survey: How many people see exactly what’s wrong with the above statement?

    I’m in the middle of rebuilding a project from the ground up because the idiots before me “repurposed” literally hundreds of variables because they could. Personally, if you need to either change the underlying type or reason for a variable to exist then you should create a brand new one and move on. If your pull this garbage then you need to go back to school and learn some new words like D-E-S-I-G-N and R-O-B-U-S-T

  19. Barry says:

    I think you’ve missed a couple of fairly important points about PHP.

    Firstly the syntax and style owes much to Unix, perl, and shell. This lowers the barrier to entry as it is a familiar style and syntax, much like Basic is, well, basic.

    Secondly, it is free. “This isn’t an anti-PHP rant – it’s a using the right tool for the job rant.” – well, unfortunately there are a lot of people in this world, some can’t, and some won’t, pay licensing fees to Microsoft either directly or via their ISP. The same goes for the development environment. So consequently some people have to use the *only* tool available.

    Both points combined, and you get a very mixed range of ability developers who are very unlikely to ever try and use OO-principles when designing web sites.

    Finally PHP is originally from 1994, when people didn’t know any better about writing web languages (OO was fairly new as well). It’s unlikely to ever undergo the massive re-engineering that ASP received as part of .NET, and besides, there’s Mono’s implementation if people want ASP.NET style development for free.

  20. Fregas says:

    Ala’a A. Ibrahim,

    I think you missed the point. I understand being defensive of your favorite language. Karl isn’t saying that PHP is bad necessarily, but just that it fits a particular niche, that is small sites and hobbyist programmers, the same way VB6, classic ASP and similar languages did. Allow me to respond to some of your comments.

    “yes, it’s not fully OOP, but wrappers exist for all the old procedural functions, if you couldn’t find one, you can easily build one.”

    Yes, but what incentive is there for you to build such wrappers? Who has time to do that? If they are not already OO, then most developers are just going to use the plain old procedural framework thats already there. And as Karl mentioned, there isn’t much of a cultural push among the PHP community to build ANYTHING in an OO fashion, much less convert the existing framework. Part of it is the langauge, and part of it is developers using it.

    “I’m not experienced in .NET and stuff, but my colleagues at work are, well I always see them complaining about documentation.

    Now, PHP being a loosely typed language, well for me I don’t see it a problem, I like to consider it a feature, an experienced programmer can make a very good use of such feature, but newbies would could fall in it.”

    Since you don’t have that experience, it is hard to know what you are missing. I’ve used coldfusion, vb6 and classic asp and i can tell you i dont want to go back to them having used .NET. Strongly typed languages tend to make refactoring and bug fixing a lot easier on midsize to large projects.

    “Also PHP is an interpreted language, which turns agile methodologies a much simpler task than complied languages, you don’t have to compile each time you run a test, you just run, and fix immediately.”

    I dont’ think be interpreted necessarily makes something agile. I kind of understand your point, how being able to save and refresh the page gives you “immediate feedback” which is an agile principle. Also, you can do that in ASP.NET…you dont’ necesarily have to recompile the whole project. It depends on how you like to work. However, interpreted languages have this disadvantage: You dont know that an error exists until you actually run that page or screen or piece of code. THis can come back to bite you later. Strongly typed langauges aren’t perfect, but at least when you compile the whole project, you know that there are not any syntax or type related errors. with interpreted langauges, you get no such feedback unless you run every piece of code.

  21. SirMike says:

    Nice article. I’ve written many web apps either using PHP and ASP .NET. I must agree with all stuff you’ve described. Writing something “bigger” in PHP is a real pain. Omnipresent procedural approach is not so convenient and it doesn’t speed up writing the code.
    The worse thing is that every major release of PHP differ so much from its predecessor. I started learning PHP when third release was “on top”. Fourth version of PHP was different and current, fifth version, presents another approach to creating applications. For somebody who starts learning PHP now it doesn’t matter but many older programmers have problem when they must learn new features in a totally different way.

  22. Well, thank you for this nice article, but let me disagree with you.
    PHP is relatively an easy language to program with, this is what brought up the inexperienced programmers (so you see a lot of the spaghetti code around), but it’s a very powerful language for the experienced.
    yes, it’s not fully OOP, but wrappers exist for all the old procedural functions, if you couldn’t find one, you can easily build one.
    Tools, well if you take a look at the Zend Studio, or PHPEclipse, I guess they are a very powerful IDEs that can make your day much easier.
    Now, one of the most important features about PHP, is the manual, you can never get lost about something built in the language, unlike other languages, PHP has a great manual, good examples and descriptions.
    I’m not experienced in .NET and stuff, but my colleagues at work are, well I always see them complaining about documentation.
    Now, PHP being a loosely typed language, well for me I don’t see it a problem, I like to consider it a feature, an experienced programmer can make a very good use of such feature, but newbies would could fall in it.
    Well, I don’t know which community you are talking about (maybe the PHP newbies community) but unit testing and Patterns and other agile stuff are widely known in the PHP community.
    Also, searching the Internet for a PHP script to do something is so easy, and you always get the source code, you can almost find any script that does things for you in PHP.
    Also PHP is an interpreted language, which turns agile methodologies a much simpler task than complied languages, you don’t have to compile each time you run a test, you just run, and fix immediately.
    Thanks for the article, it was really nice.