Type the List: A Proposal to support type-casting in PHP’s list language construct

I wrote recently about some changes that I’d like to propose for list() in core PHP for version 8.3: namely that it should work with iterables as well with arrays, and that it should allow a variadic “argument” that would assign all entries that were part of the original array being listed, but that weren’t explicitly assigned in the list; and another proposal to support default values list assignments when an element was missing from the array.

Those weren’t the only changes that I would like to see implemented for list().

Another situation that I find with list is if I want to type those variables. There are a number of PHP functions that I use regularly that return an array of string values such as preg_match() and the resultant $matches, and I can assign those matches directly to variables using list().

Continue reading

Posted in PHP | Tagged , , , , | 2 Comments

Default the List: A Proposal to support default values in PHP’s list language construct

I wrote recently about some changes that I’d like to propose for list() in core PHP for version 8.3: namely that it should work with iterables as well with arrays, and that it should allow a variadic “argument” that would assign all entries that were part of the original array being listed, but that weren’t explicitly assigned in the list.

Those weren’t the only changes that I would like to see implemented for list().

If we used list() to extract entries that don’t exist in the array:


$data = [1 => 'A', 2 => 'B'];

[1 => $firstValue, 2 => $secondValue, 3 => $thirdValue] = $data;

var_dump($firstValue, $secondValue, $thirdValue);

then this will trigger a Notice in PHP 7, promoted to a Warning in PHP 8, and assign a null value to that variable. It becomes even more problematic if we’re assigning those extracted elements to class properties that aren’t nullable:


class Foo {
    private int $firstValue;
    private int $secondValue;
    private int $thirdValue;

    public function __construct(array $data = []) {
        [$this->firstValue, $this->secondValue, $this->thirdValue] = $data;
    }
}

$data = [1, 2];

$foo = new Foo($data);

We still get the Warning (or Notice), but this will also now result in a Fatal Uncaught TypeError because we’re trying to assign that null to the $thirdValue property, which doesn’t allow nulls; making it a less than ideal situation.

So what can we do to prevent this?

Continue reading

Posted in PHP | Tagged , , , | 3 Comments

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 , , | 1 Comment