Tag: php
-

Using PHP property hooks wisely
PHP 8.4 included a new feature for class definitions called “property hooks“. These are a way to dynamically control the behavior of property reading and writing an object property. To put it another way, they let you turn property access into method calls. This is something that many other programming languages do, and it’s nice…
-

The making of phpcs-changed
Automattic developers have always held the WordPress PHP coding guidelines as the basic advice for writing WordPress.com code. However, when I started in 2013 most of the code I actually encountered in the codebase did not follow those guidelines. When I transitioned to my current team around 2016, the codebase had no automated linting at all. Having seen…
-

Refunding linked subscriptions
Today I wanted to write about another work project that’s taken up a lot of my time: how to properly refund subscriptions that were paid for over multiple transactions. Refunding an upgraded subscription Let’s say you sign up for a subscription at a service’s Basic tier for $10, and then later upgrade that subscription to…
-

The Receipt Item Tag Trick
I’m not sure I should be proud of this solution, but I wanted to discuss a technique I invented to solve a tricky problem at work. Our billing receipt database records have a separate table called “receipt tags” which are just simple strings that can be associated with a receipt. These are useful for all…
-

Commas and periods make cents
A long time ago (relatively speaking), all prices displayed to users on my company’s website were rendered from their numeric data (like 10.25) into a formatted string in PHP (like £10.25) and then passed along to the client. As our frontend began to have more complex requirements for the way prices were displayed, this was…
-

Thirty days hath September
Here’s a riddle for you: what’s one month past January 31? Is it February 28, the end of the month? Or is it March 2, thirty days ahead? Or March 3, thirty-one days ahead? There’s no correct answer. So if you signed up for a monthly subscription on January 31, or on October 31, or…
-

Record Objects to the Rescue
Very often, I need to return multiple values from a function in PHP. Maybe I need three different dates or an integer and a string. Perhaps the most common scenario is when returning data from a database query. In each of these situations I have to decide the best way to encapsulate those values. Mostly,…
-

Good coding practices
I recently had to give a talk on “Good coding practices” for some new developers at Automattic. At first I had no idea how to summarize such a vast and dynamic topic, but as I was working on a plan I started to see some ideas come up over and over again and I decided…
-

grepdef: a quick way to search for definitions in code
I live inside my code editor all day and something that I need to do pretty often is to search for the definition of a symbol (a variable, constant, or function). For example, I might be debugging a problem in a JavaScript file when I come across this code: I need to see translateDataFromRaw to…
-

Alternatives to Else
One of the first imperative programming concepts I ever learned was if/else. With this relatively simple power tool I could make decisions in my code based on any number of factors. Of course, my early programs were… a little hard to read. I hadn’t yet learned one of the maxims of programming that I try…