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

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