|  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: 2011-01-01 16:03 UTC
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: a dot dotreppe at aspyct dot org Assigned:
Status: Open Package: Class/Object related
PHP Version: 5.3.0 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: a dot dotreppe at aspyct dot org
New email:
PHP Version: OS:


 [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: *
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Jul 21 13:01:25 2019 UTC