Splat the List: A Proposal to support Variadics in PHP’s list language construct

I was thinking recently about how useful PHP’s splat operator (...), also known as variadics or array packing/unpacking, can be. I’ve written about variadics before, here and here. Variadics are incredibly powerful and useful; but there are still some limitations with them that I find frustrating. Although, to be fair, the limitation that I’ve encountered here is probably more related to list() than it is to variadics. I’ve also written recently about the list() language construct, and some of the ways it can be use.

A common situation that I’ve encountered a few times now is when I receive an array of data values, need to extract the first few elements from that array into individual variables, and retain the rest of the array “as is”. Put simply, if my initial array is as basic as $initialArray = [1, 2, 3, 4, 5], I want to be able to extract $firstValue = 1, $secondValue = 2, and $remainingValues = [3, 4, 5].


$initialArray = [1, 2, 3, 4, 5]
// do something here
// and the result that I'm looking for is:
//    $firstValue = 1;
//    $secondValue = 2;
//    $remainingValues = [3, 4, 5];

So what are my options for doing something like this in PHP?

Continue reading

Posted in PHP | Tagged , , , , , | 3 Comments

List-o-mania

The list() language construct is one of the most powerful constructs in PHP, allowing you to assign one or more elements from an array to specific named variables in a single step; but it may not work in quite the way that you would expect.

Let’s start with a simple example, a basic array of values, and we’ll use list() to extract the first two values from that array; I’m using short list syntax here for brevity (and because I feel that it’s cleaner), but the functionality is identical:


$data = ['A', 'B', 'C', 'D', 'E'];

[$firstValue, $secondValue] = $data;

var_dump($firstValue, $secondValue);

which gives us


string(1) "A"
string(1) "B"

and that’s the behaviour we would expect. Using list() has extracted the first and second elements from our array into the variables $firstValue and $secondValue… or has it?

Continue reading

Posted in PHP | Tagged , | 4 Comments

PHP 8.2 – The Release of Deprecations

The release date for PHP 8.2 has been announced, with General Availability set for the 24th November 2022; the release managers have been elected with Ben Ramsey (@ramsey) as the “veteran” supporting Pierrick Charron (@adoyy) and Sergey Panteleev (@s_panteleev) as the two “rookies”; and the first alpha release is just three weeks away. So now it’s time to start looking ahead at what this new release will bring.

And the honest answer is, very little.

So far we can look forward to three new features (one significant, the other two are relatively minor), an element of new syntactic sugar, and a mass of deprecations.

Continue reading

Posted in PHP | Tagged , , , | Leave a comment

Cassie’s First Night

It’s been a long time since I last finished any of the short stories that I write. This short piece was written 8-years ago. For some reason, I was reminded about it just this evening.

It isn’t that I don’t have the story ideas, or that I don’t start writing them down: I have dozens of part written pieces just waiting for me to continue working on them, to complete them. it’s simply a case of never finishing the writing that I start these days. Perhaps it’s because I’m never really satisfied with what I’ve written. Perhaps I should make a more positive effort to complete some of my stories.

But in the meanwhile, here’s the short story that reminded me how much I should finish my writings, (inspired by a picture that crossed my timeline); and despite the title, it’s not pornographic or NSFW: if anything, it’s a bit too middle class and innocent.

Continue reading
Posted in Fiction | Tagged | Leave a comment

Support the whole PHP Ecosystem

Just a few short weeks ago, for a 24 Days in December post, I wrote about remembering all of the Open Source Maintainers that create and support the tools that we all use as developer in our daily work. It isn’t always the obvious tools and frameworks (PHP itself, PHPStorm, Symfony, Laravel, Composer, XDebug, PHPUnit) that make a difference; there are so many unsung heroes that work in the background making our work so much easier.

Continue reading

Posted in PHP | Tagged , , | Leave a comment

Reflections on Thought and Expression

For me, it’s been a particularly difficult and traumatic last few months of 2021. Not simply because of the whole covid situation, or the usual stresses of work; but because I started taking therapy over the Summer. I know that I’m a mess of hang-ups and inhibitions, and have a lot of difficulty expressing myself emotionally: I’ve spent over two years trying to address those issues with a variety of self-help techniques; but with very limited success. So I finally realised that I needed professional help if I was going to make myself more open.

I found a therapist here in Amsterdam that takes on English-speaking clients; and feel like I’ve been making progress with her. It’s been a painful experience, she seems to have an instinct for homing in on the things that I don’t say; and perhaps it’s the traditional Dutch bluntness, but she’s exposed a few memories that my mind has preferred that I forget, and made me explore them and the effects that they’ve had on me. One in particular going back to my teenage years; another from the mid-90’s when I was living in Manchester; and most recently a third incident in my life from about 25 years ago, which is germane to this post.

I’d just had a particularly difficult week, and received some news that I was dreading; and my mind simply couldn’t focus or concentrate on anything; so I decided to treat myself to a spa day to help clear my mind. I booked in for a full day at Spa Zuiver near Amsterdamse Bos, complete with meals and a relaxation massage; and found that they had a regular schedule of löyly (or aufguss) sessions in the Finnish sauna, which I particularly enjoy. The smell of the scented oils, the blasts of heat: it’s a wonderful, invigorating experience. It was also fun trying to identify the aromas; and amusing that I was identifying the individual smells (cinnamon, nutmeg, cloves, ginger, almonds) aloud in English; and the guy on the bench next to me was putting the ingredients together to get speculaas. The last oil was the trigger for me: I identified anise and liquorice, and when I realised it was sambuca, I had to leave the sauna early to get some cool, fresh air. I don’t drink alcohol, I haven’t done so in over 30 years; but that wasn’t the issue: my mind was just swamped by memories of that incident from 25 years ago.

I’d spoken with my therapist about my intent to visit the spa in my previous session; so naturally in my next therapy session she asked how it had gone: my telling her about walking out of the aufguss is where things got particularly interesting.

Therapist > But you said that you always enjoy the aufguss; so why did you leave the sauna early?
Me > It’s hard to explain; but it triggered a wave of memories that just overwhelmed me, and I needed to escape.
Therapist > Try to put it into words for me. What was your Inner Monologue saying to you?
Me > My Inner what?!?

That’s when she had to explain to me what an Inner Monologue was. And it took some explaining; because I’ve never experienced an Inner Monologue, so the whole concept of the mind giving a verbal commentary on its thought processes was very difficult for me to understand.

Continue reading

Posted in Personal | Tagged , , | Leave a comment

The Wonderful World of Callbacks

Anybody that has read any of my previous blog posts or looked at my code samples before should know that I’m a big fan of using callbacks. Whether it’s simply using callbacks with those standard functions that accept them as an argument (such as the functional tripos of array_map(), array_filter() and array_reduce()) or customisable array sorting through usort() and its relatives, building classes with methods that accept callbacks, or the darker magic of binding closures to a class or instance to give it new functionality. I always find that callbacks allow me to build applications with a great deal of flexibility.

Where the logic of the callback is simple (such as filtering an array or iterable), I typically used inline functions; although since the introduction of arrow functions in PHP 7.4, I have been replacing many of my one-liner functions using them instead. If I need something more complex, I might use an anonymous function rather than defining it inline; or if it’s a function that will be called more regularly, or that incorporates business logic that should be maintained separately, then I’ll create it as a named function or method rather than defining it anonymously, so that it’s a separate entity in the codebase, maintained with all the other business logic, and can still be called as a callback.

Continue reading

Posted in PHP | Tagged , , , , , | 1 Comment

ReadOnly Gotchas – A few more limitations with PHP 8.1 ReadOnly Properties

Last month I wrote about the new readonly properties in PHP 8.1, and the difficulties if you want to clone an object that uses them (together with a couple of potential solutions to that problem). The inability to clone an object with readonly properties isn’t the only limitation that you might encounter though; and while the other limitations are mentioned in the PHP documentation, they could easily be overlooked.

Continue reading

Posted in PHP | Tagged , , | 2 Comments

Constant Constants. Finally! (On the inconstancy of constants)

One of the many new features of PHP 8.1 is the ability to declare class constants as final, so that they can no longer be overridden in child classes. The same applies when constants are defined as final in an abstract classes, or interface; they can’t be overridden by classes extending that abstract or implementing that interface. So class and interface constants can now truly become constant.

You could be forgiven for believing that a constant will always retain the same value throughout, it’s in the name, right? And technically that’s correct. But it all comes down to context, to the class (or interface) where that constant is defined, and its visibility; and when it comes to class inheritance, all bets are off. The value of a constant can be defined at one level within the inheritance tree; but overridden at another level. Technically, it’s a different constant; but having constants with the same name yet different values within a class inheritance tree can certainly create confusion, and potentially lead to awkward bugs. The PHP documentation does note that this can be the case; but perhaps it really should be a warning instead.

Continue reading

Posted in PHP | Tagged , , , , | 1 Comment

The Tears of a Clone

On the face of it, the new readonly properties in PHP 8.1 seem really useful for Value Objects and Data Transfer Objects: both should be immutable; setting property values should always be handled in the constructor, so no setters; and with readonly properties, we no longer need to code boilerplate getter methods; so we only need to code a comparison method for value objects. Between constructor property promotion introduced in PHP 8, and readonly properties introduced in PHP 8.1, we can reduce the code (skipping the comparison for brevity) for defining a Money class from


class Money {
    protected int $value;
    protected Currency $currency;

    public function __construct(int $value, Currency $currency) 
    {
        $this->value = $value;
        $this->currency = $currency;
    }

    public function getValue(): int {
        return $this->value;
    }

    public function getCurrency(): Currency {
        return $this->currency;
    }
}

to a much more simplistic


class Money {
    public function __construct(
        public readonly int $value,
        public readonly Currency $currency
    ) {}
}

and then we directly access the value and currency of the object externally as object properties, rather than needing to use the getter methods of the original version.

The new version of our Money class looks so much cleaner: we’ve reduced the cognitive overhead, and we’ve eliminated a lot of boilerplate code – readonly properties FTW, right?

Continue reading

Posted in PHP | Tagged , , , | 2 Comments