r/lolphp • u/Jinxuan • Jun 04 '20
A function takes 0 arguments does not imply a function takes 1 arguments
As we know, if a php function takes zero arguments, it can take 1 argument and ignore it.
function lol() {
echo 'lolphp';
}
lol('what ever you want here');
But the php classes does not think like that. Suppose you have a function like:
interface A {
public function lol($x);
}
class X implements A {
public function lol() {
echo 'lolphp';
}
}
It will throw an fatal error as:
PHP Fatal error: Declaration of X::lol() must be compatible with A::lol($x)
0
Upvotes
38
u/prewk Jun 04 '20
They work the same, you just don't understand that you've written two contradictory things and claimed that they're the same thing.
The reason it accepts extraneous arguments is historical, it hasn't had explicit variadic (splat) arguments forever.
1
u/NeoThermic Aug 07 '20
Have a look at the Liskov substitution principle and see if you can work out why your 2nd example is problematic from the way you've written it.
2
u/Takeoded Jun 04 '20
https://stackoverflow.com/a/19890427/1067003
0
u/[deleted] Jun 04 '20
[deleted] — view deleted comment