php.net |  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
Votes:4
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
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
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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-01 16:03 UTC] jani@php.net
-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.

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
 [2020-10-04 04:07 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2020-10-04 04:07 UTC] requinix@php.net
@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.
https://wiki.php.net/rfc/howto
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC