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

Closure Binding for Unit testing of Private and Protected Methods

Some years ago, I wrote about using closure binding to access protected and private properties when unit testing an object, to verify internal state; and I created the SpyMaster library to simplify that task. One feature that I didn’t include in the version of SpyMaster that I released publicly on github was the ability to execute protected and private methods within an object.

I know that a lot of developers will declare internal methods as public if they want to make them testable; but defining methods that are supposed to be internal to a class as public is rarely a good idea, because it is then a part of the public API for the class, and can be accessed from external code.
Other developers might use Reflection in their unit tests to make private or protected methods executable. That’s a better approach, because the class under test retains its internal methods as protected or private.

An alternative approach is to use closure binding to execute those methods without needing to change their accessibility at all, similar to the way that the original SpyMaster library accesses internal properties without changing their visibility.

Continue reading →

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

Spreading the News – An Exploration of PHP’s Spread Operator

A few days ago, I wrote about array unpacking and variadic arguments in PHP8. I’m a big fan of these features, and use them extensively, and the changes in PHP8 make them even more useful.

Continue reading →

Posted in PHP | Tagged , , , | 1 Comment

Named Arguments and Variadics in PHP 8

One of the new features of PHP 8 is named arguments. It’s one of those features that I love as an end user developer; although it can be a nightmare for library and framework developers, because argument names are now part of the API for public methods, not simply the argument types, and any change to an argument name becomes a backward-compatible break.

Continue reading →

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

Loving you wasn’t easy.

Loving you wasn’t easy.

Continue reading →

Posted in Personal | Tagged | Leave a comment

Confronting our Demons

Confronting our own inner demons is the hardest battle we ever have to face, especially when they’re so deep-rooted after so many years: filling in the holes that each leaves behind with hope, because that’s all that I have at the moment. I’m feeling a little like Pandora’s Box.

Continue reading →

Posted in Personal | Tagged | Leave a comment

A Reverse-Retrospective: Places that I’m looking forward to visiting in 2021

With all the closures and travel restrictions in place during last year, there are so many places that I wanted to visit, but wasn’t able to. So rather than a look back at last year (with so much trauma and negativity), I’m going to look forward (and positively) to where I’ll be able to go this year as lockdown eases, and travel becomes possible again. There are so many places in Europe and across the world that I want to go, but I’m going to limit myself to the Netherlands at the moment, places just a few hours drive or train journey from where I live.


Continue reading →

Posted in Uncategorized | Tagged , | 1 Comment

Under the Radar? A Backwards-Compatible Break for SPLFixedArray in PHP 8

The official release date for PHP 8 is 26th November, just 9 days away, so we’re almost into the last week; and I’ve just discovered a change to SPLFixedArray that I wasn’t previously aware about. While not many developers use SPL Datastructures, and probably fewer still use SPLFixedArray, it is a big BC break; and it wasn’t something that I had seen documented as a PHP 8 change.

Continue reading →

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

An Attempt at Poetry

It’s been nearly two months since my last post, two months trying to pull myself out from the state that I’d got myself into. 2020 isn’t a forgiving year, and I’m hurting myself and others with the darkness that I’ve found myself facing.

So I’m trying to express some of my emotions in poetry (of a sort) in English, and also in Dutch.

Continue reading →

Posted in Personal | Tagged , | Leave a comment

Fear and Self-Loathing in Amsterdam

 

I must not fear.
Fear is the mind-killer.
Fear is the little-death that brings total obliteration.
I will face my fear.
I will permit it to pass over me and through me.
And when it has gone past I will turn the inner eye to see its path.
Where the fear has gone there will be nothing.
Only I will remain.

– Bene Gesserit Litany Against Fear, Dune series of books, written by Frank Herbert

As an Englishman of a certain generation, I was brought up to believe that it was bad to show emotions, that I should keep everything bottled up inside… “stiff upper lip”, “it just isn’t the done thing”, “it’s a sign of weakness”, and all that. If you’re scared, then you should put on a brave face: if things are going badly, then still look cheerful and don’t let anybody know how unhappy you are.

Continue reading →

Posted in Personal | Tagged , | 1 Comment