Req #49261 [Com]: Default arguments on interface implicitly passed to implementing classes
| From: | corey dot taylor dot fl at gmail dot com | Date: | Sun, 04 Oct 2020 03:42:59 +0000 |
| Subject: | Req #49261 [Com]: Default arguments on interface implicitly passed to implementing classes | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-229360@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=49261&edit=1
ID: 49261
Comment by: corey dot taylor dot fl at gmail dot com
Reported by: a dot dotreppe at aspyct dot org
Summary: Default arguments on interface implicitly passed to
implementing classes
Status: Open
Type: Feature/Change Request
Package: Class/Object related
Operating System: *
PHP Version: 5.3.0
Block user comment: N
Private report: N
New Comment:
I don't know that the default value should be implicit. However, since this is the closest bug
on the topic, I wonder if the default values should be part of the signature.
If you define a default value in the interface only, then a mismatch error is thrown.
https://3v4l.org/L94uR
However, if you do define a default value in the class but not the same value, then no error is
thrown.
https://3v4l.org/VGpUa7
Previous Comments:
------------------------------------------------------------------------
[2009-08-14 19:56:41] a dot dotreppe at aspyct dot org
Description:
------------
Hi,
The aim of interfaces is to provide the same external behavior, no matter what class implements it.
Maybe it would be nice for interfaces to be able to force a default value for argument on
implemented methods.
Moreover, that way we could change all method's default argument by modifying it on one place
only: the interface.
Reproduce code:
---------------
interface Fetchable {
const FETCH_ASSOC = 1;
function fetch($fetchStyle=self::FETCH_ASSOC);
function fetchAll($fetchStyle=self::FETCH_ASSOC);
}
class PDOLayerStatement implements Fetchable {
/* This one works fine, but I have to know the default value on interface,
* and even reference the interface,
* which will raise the amount of maintenance work
*/
public function fetch($fetchStyle=Fetchable::FETCH_ASSOC);
/* These two won't work, sadly :( */
public function fetch($fetchStyle) {}
public function fetchAll($fetchStyle) {}
}
Expected result:
----------------
It would be nice if PHP could automatically give Fetchable::FETCH_ASSOC as default value for
$fetchStyle in every implementing class.
Actual result:
--------------
Fatal error: Declaration of PDOLayerStatement::fetch() must be compatible with that of
Fetchable::fetch() in C:\xampp\htdocs\aspyct\src\test.php
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=49261&edit=1