Why I love/hate PHP

Cartoon image of a frustrated programmer sitting at a computer
I’ve been slinging PHP code since the late 90s (PHP3 era). My name’s in a half-dozen Wrox Press books as a technical reviewer, covering Linux, MySQL, PHP4. (I’m even on the cover of one, as an author.) As a quick, dirty way to get a website up and running, it’s ... well, quick and dirty. But it was kind of obviously hacked together, and there’s a lot of inconsistency. For example, in the strpos() call, the arguments are in the order haystack, needle, while in in_array() it’s needle, haystack. If you’re not using PhpStorm (and I’m usually not), with API autocompletion, that may not be that big of a deal, but if you’re logged in remotely using vim or otherwise not wrapped in an IDE, the mental task switch of flipping over to the API reference to see which order a particular call requires is, for me, less than ideal. 

But for all its foibles, it’s still, in my opinion, one of the best ways to rapidly get a web application up and running. Historically, it has been loosely typed, and once you have Apache and PHP installed, you’re good to go, there are no other frameworks to install, no IDE or compiler is strictly needed, etc. Easy.

So not only have I used it here and there, I’ve taken some pains to reinvent certain wheels to make it even easier. (E.g., I rescued PHPHTMLTemplate (itself a port of the Perl module HTML::Template), implemented a PHP approximation of CGI::Application, and have written at least most of a database abstraction layer “inspired by” Class::DBI.) And I’ve written some of my own personal use apps in those frameworks.

Everything was running fat, dumb, and happy on PHP 7.3, but PHP (like Python, albeit maybe to a lesser extent) loves to break things. And if you’re a hobbyist, you may not be up on all the changes.

In PHP5, there are two ways you can write a constructor method inside a class. The first one is to create a method with the name __construct() inside the class. The second is to create a method naming exactly the same as class name. For example if your class name is Emailer, the name of the constructor method will be Emailer(). 

Hayder, Hasin, Object-Oriented Programming with PHP5 (Packt Publishing Ltd., 2007), p. 24. 

I’ve always used __construct() (and getters and setters, vs. dynamically sticking attributes onto an object in running code), but the original author of the PHPHTMLTemplate code used the “class name” method. And when I upgraded my system from 7.3 to 8.2 to fix a different dependency, everything broke. It took me a minute to figure out why. There were no warnings about the deprecated nomenclature, just code weirdness where objects were null when they should have been instances of a class. There were a number of other tweaks I had to make to the inherited codebase, which is now cleaner and more robust, in my opinion (functions are no longer being called with null values when the don't need to be, dynamic properties are no longer being used, etc). But it was a bit frustrating when suddenly nothing worked.

 Yes, I should have setup a beta or test environment and done regression testing before making the change to the underlying software. Maybe I should dockerize everything and have interchangeable server environments, so dropping back to 7.3 from 8.2 could have been done in situ. Etc. All valid points that I thought about after the fact, since this stuff is, again, a hobby, not a vocation. But, well, yeah.

 

 

 

‡ It’s great software, I just think it’s a bit expensive for a hobbyist like me, at $99 for an individual (though that at least reverts to a “perpetual license” at the end of the year), with no free version for open source contributors, etc. (they offer that for some of their products).

Comments