|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49261 Default arguments on interface implicitly passed to implementing classes
Submitted: 2009-08-14 19:56 UTC Modified: 2020-10-04 04:07 UTC
Avg. Score:4.5 ± 0.9
Reproduced:4 of 4 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: a dot dotreppe at aspyct dot org Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.3.0 OS: *
Private report: No CVE-ID: None
 [2009-08-14 19:56 UTC] a dot dotreppe at aspyct dot org

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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-01 16:03 UTC]
-Package: Feature/Change Request +Package: Class/Object related -Operating System: Windows Vista 32bit +Operating System: *
 [2020-10-04 03:42 UTC] corey dot taylor dot fl at gmail dot com
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.

However, if you do define a default value in the class but not the same value, then no error is thrown.
 [2020-10-04 04:07 UTC]
-Status: Open +Status: Not a bug
 [2020-10-04 04:07 UTC]
@OP: I think the confusion between an interface method clearing stating that the parameter is optional and the class method seemingly stating it is not, plus the non-obvious behavior that calling the class's method without the "required" argument is *not* incorrect, far outweighs the benefits.
Also, it's been 11 years.

@corey: Whether the default values should be part of the definition is a matter for the RFC process.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jun 20 10:01:31 2024 UTC