|  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
Have you experienced this issue?
Rate the importance of this bug to you:

 [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-2020 The PHP Group
All rights reserved.
Last updated: Tue Mar 31 14:01:23 2020 UTC