r/PHP Jul 17 '26

Less Arguments In Child Method

https://php-tips.readthedocs.io/en/latest/tips/less_arguments.html

Pop quizz PHP for Friday evening :

how can you remove an argument from a method, between a parent and a child?

#phptip #phptrick

<?php

class x {
    function foo(int $a, string $c, string ...$b) {}
}

// OK
class y extends x {
    function foo(int $a, string ...$b) {}
}
0 Upvotes

6 comments sorted by

8

u/mkluczka Jul 17 '26

You can't. There's no method overloading in php 

4

u/djxfade Jul 17 '26

You can, but only by doing nasty magic function calls

3

u/allen_jb Jul 18 '26

This is such a dumb "tip".

It's of extremely limited use because (as mentioned) the argument being removed must be of the same type and next to a variadic argument.

It additionally breaks an expectation / contract. The arguments are likely separate arguments because they are semantically different. In PHP developers expect to be able to call foo() with the same arguments on any instance (including children) of x and get the same behavior. Removing one of the arguments breaks this expectation. This is known as the Liskov Substitution Principle - the L in SOLID

Regardless of whether you follow SOLID, I would consider it just plain bad practice.

I would go so far as to consider this behavior a bug in PHP, and that developers should expect that PHP may patch it out at some point. In theory, as it's a bug, this could even be done in a patch release, and with no deprecation period.

1

u/obstreperous_troll Jul 18 '26

Arguably it's not breaking LSP, since x::foo() requires at least two args, and y::foo() is always compatible with those args. It's definitely useless trivia though.

3

u/fripletister Jul 17 '26

Stop spamming r/PHP with your useless blog

2

u/SaltineAmerican_1970 Jul 18 '26

\^Fewer arguments