# Signature compatibility: Number of arguments

- Thread: https://externals.io/message/68675
- Original archive: https://news-web.php.net/php.internals/68675
- Started by Nikita Popov on 2013-08-29T09:33:24+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #68675 — Nikita Popov — 2013-08-29T09:33:24+00:00

URL: https://externals.io/message/68675#68675

Hi internals!

This is a spinoff from the variadics thread. Quoting Stas:

> I also think this:
>     public function query($query, ...$params)
>     public function query(...$params)
> should be legal too.

This is a general issue in PHP that we might want to fix: Currently a
method A is not compatible with a method B if A has less arguments than B.

E.g. both of the following are incompatible signatures:

    public function foo($bar)
    public function foo()

    public function foo($bar = NULL)
    public function foo()

This doesn't really make sense. Removing parameters doesn't violate LSP,
because in PHP it is legal to pass more arguments than declared. (Only
adding additional required parameters would violate LSP.)

Is it okay to change this?

Thanks,
Nikita

---

## #68676 — Nikita Popov — 2013-08-29T10:53:23+00:00

URL: https://externals.io/message/68675#68676 · In reply to: #68675

> This is a general issue in PHP that we might want to fix: Currently a
> method A is not compatible with a method B if A has less arguments than B.
>
> E.g. both of the following are incompatible signatures:
>
>     public function foo($bar)
>     public function foo()
>
>     public function foo($bar = NULL)
>     public function foo()
>
> This doesn't really make sense. Removing parameters doesn't violate LSP,
> because in PHP it is legal to pass more arguments than declared. (Only
> adding additional required parameters would violate LSP.)
>
> Is it okay to change this?
>
> Thanks,
> Nikita
>

Patch for this change: https://github.com/php/php-src/pull/424

Nikita

---

## #68679 — Patrick ALLAERT — 2013-08-29T11:14:11+00:00

URL: https://externals.io/message/68675#68679 · In reply to: #68675

2013/8/29 Nikita Popov <nikita.ppv@gmail.com>:
> Hi internals!
>
> This is a spinoff from the variadics thread. Quoting Stas:
>
> > […]

I respectfully disagree.

The first method requires at least one parameter (or two, depending on
the pending clarification in the other thread) , and the latest: zero
or more (or 1 or more).

> This is a general issue in PHP that we might want to fix: Currently a
> method A is not compatible with a method B if A has less arguments than B.
>
> E.g. both of the following are incompatible signatures:
>
>     public function foo($bar)
>     public function foo()
>
>     public function foo($bar = NULL)
>     public function foo()
>
> This doesn't really make sense. Removing parameters doesn't violate LSP,
> because in PHP it is legal to pass more arguments than declared. (Only
> adding additional required parameters would violate LSP.)
>
> Is it okay to change this?

I see no reason to do so and I would say -0.5.

---

## #68688 — Levi Morrison — 2013-08-29T15:57:12+00:00

URL: https://externals.io/message/68675#68688 · In reply to: #68676

I think of the following definition:

function foo($bar = NULL)


To really be two definitions with method overloading:


> function foo()

function foo($bar)


Allowing an inheritor/implementor to choose to not implement the second
signature does not seem logical at all when you think of them this way. I'm
against this this proposal.

---

## #68691 — Stas Malyshev — 2013-08-29T17:15:45+00:00

URL: https://externals.io/message/68675#68691 · In reply to: #68679

Hi!

> I respectfully disagree.
> 
> The first method requires at least one parameter (or two, depending on
> the pending clarification in the other thread) , and the latest: zero
> or more (or 1 or more).

Yes, this is true. But how it is an objection? LSP allows to weaken
preconditions, but not to strengthen them - that's exactly what happens.
Any method call that worked for base class would also work for derived
class, and some other, additional calls, would work too.

> > […]
> 
> I see no reason to do so and I would say -0.5.

The reason is that current situation does not match LSP requirements and
serves no useful purpose. Especially the second one which literally has
the same preconditions but for some weird reason is rejected.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #69182 — Nikita Popov — 2013-09-18T10:30:01+00:00

URL: https://externals.io/message/68675#69182 · In reply to: #68675

> Hi internals!
>
> This is a spinoff from the variadics thread. Quoting Stas:
>
> > […]
>
> This is a general issue in PHP that we might want to fix: Currently a
> method A is not compatible with a method B if A has less arguments than B.
>
> E.g. both of the following are incompatible signatures:
>
>     public function foo($bar)
>     public function foo()
>
>     public function foo($bar = NULL)
>     public function foo()
>
> This doesn't really make sense. Removing parameters doesn't violate LSP,
> because in PHP it is legal to pass more arguments than declared. (Only
> adding additional required parameters would violate LSP.)
>
> Is it okay to change this?
>
> Thanks,
> Nikita
>

Considering that we might support named parameters in the future I'm
withdrawing this suggestion. This allows removing a parameter name, which
would cause problems in that context.

Nikita

---

## #69188 — Julien Pauli — 2013-09-18T12:56:04+00:00

URL: https://externals.io/message/68675#69188 · In reply to: #68675

> Hi internals!
>
> This is a spinoff from the variadics thread. Quoting Stas:
>
> > […]
>
> This is a general issue in PHP that we might want to fix: Currently a
> method A is not compatible with a method B if A has less arguments than B.
>
> E.g. both of the following are incompatible signatures:
>
>     public function foo($bar)
>     public function foo()
>
>     public function foo($bar = NULL)
>     public function foo()
>

I dont really agree.

One could use reflection on the parameter, thus expecting it to be here on
a child instance :

class Foo
{
    public function bar($arg = null)  {  }
}

class Foo2 extends Foo
{
    public function bar()  {  }
}

$o = new Foo; /* Switching to Foo2 will lead to an Exception */
$r = new ReflectionParameter(array($o, 'bar'), 'arg');
echo $r->getName();


PS : I remember having a long discussion with Gustavo, some time ago, about
LSP in PHP ; and we could not agree on some points, particularly concerning
optional parameters.

Julien.Pauli
