June 7, 2018

Dynamic Language Experience While Learning PHP

I'm learning PHP. Technically I'm re-learning it, as I learned it many years ago (version 5.0 if I remember correctly), but between the passage of time ravaging my memory and all of the new features in it and the infrastructure around it, it feels like learning it for the first time.

I understand that the accepted thing to do with PHP is complain about it instead of writing programs in it, but I have no intention of doing that. PHP is a good language that is constantly improving and it has some amazing tools springing up around it. What I want to talk about is the experience of going from a static, strongly typed language to a dynamic, loosely typed one.

I'm working in PHP because reasons (as the young people like to say) and the time frame I'm trying to work with is shorter than would let me thoroughly learn one of my dream languages like Erlang or Elixir. Until six months ago, I was a Java programmer and had over 20 years of experience in the language having worked with it since version 1.0 back in the mid-nineties. I chose not to work with Java for a number of reasons, each of which deserve their own blog post, but basically I don't trust Oracle's stewardship of the Java language and Java web frameworks are generally gigantic monstrosities that I find to be a pain to program in. (I also seem to be entirely unable to overcome my irrational hatred of Spring, so that doesn't help.) The Java language itself is still nice and I continue to like it, but everything that surrounds it these days is questionable to my way of thinking.

As a corporate Java programmer I had gotten used to having certain tools available. A selection of IDEs (Integrated Development Environments), build tools, testing tools, coverage tools and code quality tools. These are all great and switching to PHP felt like leaving my safety net behind. I looked into the choices of frameworks in the PHP community, but while it looked like CodeIgniter was the best fit for me on paper, when I started looking into it, the usual challenge of having to do everything exactly the way the framework wants you to started annoying me even before I'd finished the Hello World example. The data access language that kinda looked like SQL, but wasn't actually SQL was the final push I needed to say I'll just use the standard libraries and carefully code things myself. My technical needs on the project are gentle and I honestly think that it is small enough to get away without a framework. (Certainly if I was using Java I'd be tempted to just use servlets and JSPs, maybe Struts if I got to feeling frisky! :-)

I'm taking what I'm calling an Object Functional style to my coding. I have domain objects, but they're written in an immutable style and the main code is more procedural / functional than object-oriented. This is somewhat eccentric, but that's the coding style that I have settled into after mumble mumble years of software development and it works really well for me.

Some of the challenges as I've been learning (re-learning) PHP have been self caused as I learn the basics of the syntax and simple type-system. Others have been caused by the lack of a compile phase to catch my typos. A single wrong character in most programming language statements will cause problems, but with static languages these are caught at compile time and if you compile frequently these are caught early and swiftly found and fixed. With a dynamic language that typo in your program doesn't show up until you run it to try it out and the flow of your program goes through the section with your error in it.

There are several ways to address these collective issues. Testing is one of them. I have lots of experience with testing in the Java world, so I have dug into the PHP unit testing tool PHPUnit. It's fairly straight-forward and I have been finding bugs by the bucket-load since I started writing tests. The tests help flush out some of the typos as well, but I still need to find some kind of linter that will recursively examine my code to inform me of typos or regular syntax errors. With my application being data driven, I have also been using PHPUnit to drive integration tests against my locally installed database. I'm about halfway through testing all of my data access objects (DAO's to their friends) and the total testing runtime is still around 0.6 seconds. This is very acceptable for integration tests, so I run them often because I might as well.

The biggest thing that I miss from my static programming language days is strong types. Thankfully, PHP 7.x has type hinting and you can use user-defined objects as types. This has enabled me to start specifying explicit types for method arguments and return types. With the option to enable strict types in the language, this is giving me enough typing that I think I'll survive using a loosely-typed language. And if I want to be even more paranoid, I can define my own types if I want to be super specific and use that instead of one of the built-in simple types like string. For example, I currently use a string for the country field, but I could create a country object that only accepts "USA" and "Canada" and then it'll be near impossible for the program to have an incorrect value of country. I like having the option to be able to do that. I haven't done it yet, but it's something I'm strongly considering as data integrity is a high priority for me.

I'm enjoying working with PHP and every time I figure out a way to use it more precisely I get happier still. Maybe when the application grows large enough, I'll write later modules using a PHP framework, but for now I'm enjoying the simple approach.

Tags: Software PHP Geeky