Pragmatism in the real world

Additional parameters on a PHP interface method

On the Roave Discord recently, there was a discussion about not breaking BC in interfaces inspired by this post by Jérôme Tamarelle:

Gromnan post.

It’s clearly true that if you add a new parameter to a method on an interface, then that’s a BC break as every concrete implementation of the interface needs to change their signature.

However, Gina commented that you don’t need to use func_get_arg() as concrete implementations can add additional optional arguments.

WHAT?!!!

I didn’t know this and it had never occurred to me to try, so I had to go 3v4l.org and check

<?php

interface Foo
{
    public function bar(int $a): void;
}

class Baz implements Foo
{
    public function bar(int $a, int $b=2): void
    {
        echo "$a: $b";
    }
}

(new Baz)->bar(1,3);

Sure enough, this code works!

I don’t have much of a need for this, but it’s useful to know.

Thoughts? Leave a reply

Your email address will not be published. Required fields are marked *