TheJach.com

Jach's personal blog

(Largely containing a mind-dump to myselves: past, present, and future)
Current favorite quote: "Supposedly smart people are weirdly ignorant of Bayes' Rule." William B Vogt, 2010

So, PHP...

There was yet another wave of PHP-hate across the interwebs over the last couple weeks. It's truly fascinating that a language as decent as PHP can have hatred that rivals and sometimes exceeds the hatred for BASIC and even for some people COBOL. Since I haven't written anything in a while (lots of drafts), I figured I'd write about some of the things that I like about PHP.

First off however is what I don't like about PHP, since I can't say I'm gushing happiness with the language. I don't like the syntax (with the exception of sigils and double-quote-inlining) and their choice of OOP implementation that's an unholy mix of C++ and Java. After years of Python and a few decently sized projects in Clojure, coming back to PHP feels like just a nicer version of Java. (Of course, after years of programming Java, I don't particularly hate it either...) I don't like the function argument order ambiguity and inconsistency, a problem that is mitigated through memorization and "php.net/func-name" but would be best mitigated with specifying default arguments at call-time in a way similar to what Python allows, which makes remembering the order irrelevant. I also think PHP could be faster, given I think of it as "the C of the web" (which will probably generate some hate). I hate the choice of a backslash for namespaces. I hate the lack of $_PUT and $_PATCH and $_CUSTOM and so on. Sometimes I pine for a REPL. I wish it handled utf-8 better. I wish it supported multiple inheritance.

That about sums up my issues with modern (>= 5.3) PHP. God help you if you're still supporting PHP 4 code.

I'm not turned off by the "everything in global namespace" feature. C does the same thing (albeit forces you to include the parts of the standard library you want, unlike PHP), and in both C and PHP there are ways to manage your personal namespace if it bothers you. (More so in PHP with the introduction of actual namespaces, which I've yet to use since class-wrapped scope is Good Enough for me.) The reason it doesn't bother me is because I don't use PHP as a general-purpose language, I use it as a web-language. Having access to all the important web-specific functions without including separate modules is a joy, but it would suck if I was developing a video game or something that didn't need such functions.

What I mean when I liken PHP to the "C of the web" is that it's "bare metal" in the sense that the CGI pattern of web applications is "bare metal", and PHP is just CGI with a lot of delicious sugar. The CGI-style of developing a backend is still my favorite style because it makes state-management a lot simpler. Every request gets its own PHP instance, there is no shared state between instances unless an intermediary of a database or a simple text file is used. It makes performance problems easy to identify and work around, it makes code easier to reason about, and it's just a really clean way of doing things. It makes it straightforward to move any page-level dynamic pieces out into JavaScript and cache all the server-side routes apart from data APIs. I've used the single-process-lots-of-threads model with Java and Python and Node, I don't think they're as good. They do allow for some interesting optimizations, however, so they're not always the wrong decision. Some problems even require them if you want sanity.

I like the Yii framework, to the extent I like frameworks at all. It makes MVC fairly painless and does a lot of work for you. Yii is on par with RoR and Django, though it does have its pain-points.

I like Smarty, but find it unnecessary. I still love PHP's ability to inline with regular HTML which makes it an instant template language among everything else. Not the most concise template language, but it's still pretty concise, and most web designers shouldn't be frightened by its presence whereas they might lose their marbles if they ever encounter the elegant (html (head (title "blah")) (body (div (h1 "Hi")))) you might find in a Lisp application.

PEAR is nice, sometimes.

I like that PHP is more or less the standard of web development. It's easy enough for artists to get around in, and powerful enough to allow for clever abstractions. All the cheap shared web hosts support it, along with a bunch of free hosts too. It's easy to use with Apache (such as using mod_rewrite).

I like that it grew out of imperative programming roots rather than OOP roots, I think it's a better language because of it. If you think PHP is already a ball of mud, imagine how much hairier that ball would be if all the important parts were wrapped up in classes and had no imperative counterparts. I still prefer the mysqli_ family of functions to the class and even to PDO, but of course I recommend PDO to anyone unfamiliar with anything else (and if they're not using MySQL).

Along with being a manifestation of the CGI model, I like how significantly more details of the request-response cycle are made explicit. When does the request end? When the PHP script is done. How can I make sure I do all my header-stuff before the first byte is sent? Use output buffering. What sort of request was sent, what data was sent? I can access it easily through the magic variables. phpinfo() is very useful.

I have even more but these are the important ones to me at this time. Again, some are features I wouldn't want in a general purpose language like Python or Clojure, but for web application development, where the constraints that force Erlang or Node or Java on the developer don't apply, PHP just works really well.


Posted on 2012-07-06 by Jach

Tags: php, programming

Permalink: https://www.thejach.com/view/id/255

Trackback URL: https://www.thejach.com/view/2012/7/so_php

Back to the top

Back to the first comment

Comment using the form below

(Only if you want to be notified of further responses, never displayed.)

Your Comment:

LaTeX allowed in comments, use $$\$\$...\$\$$$ to wrap inline and $$[math]...[/math]$$ to wrap blocks.