Bug #76370 [Com]: Fatal error instead of warning when extending class that implements interface

From: Date: Sun, 05 Aug 2018 08:18:03 +0000
Subject: Bug #76370 [Com]: Fatal error instead of warning when extending class that implements interface
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-216605@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76370&edit=1

 ID:                 76370
 Comment by:         Wes dot example at example dot org
 Reported by:        kinglozzer at gmail dot com
 Summary:            Fatal error instead of warning when extending class
                     that implements interface
 Status:             Open
 Type:               Bug
 Package:            Class/Object related
 Operating System:   Mac OS X
 PHP Version:        7.2.5
 Block user comment: N
 Private report:     N

 New Comment:

(assuming mixed includes all types including null)

class Foo {
    public function method(?X $arg = null) {}
}

class Bar extends Foo {
    public function method(){}
    // PHP should interpret this the same as
    // public function method(mixed $arg = null){}
}

class Baz extends Bar {
    // ...in order to disallow stuff like this:
    public function method(?Y $arg = null) {}
    // Error: parameter 1 must be mixed and be optional as in class Bar
}


Previous Comments:
------------------------------------------------------------------------
[2018-08-05 07:58:31] Wes dot example at example dot org

Not only it shouldn't be a fatal error, but shouldn't be a warning either. Parameters are
optional and untyped in all the signatures, hence going to one to the other should never cause
errors. That is because php does not reject trailing parameters.

function bar(mixed $one = null, mixed ...$foo){}
function bar(mixed $one = null, mixed $two = null){}
function bar(mixed ...$foo){}
function bar(){}

these are all compatible... because no parameter is ever required, in all of them

------------------------------------------------------------------------
[2018-05-24 08:37:31] kinglozzer at gmail dot com

Description:
------------
If a class implements an interface, any child classes method definitions must match the *parent
class* exactly (not the interface) or a fatal error will occur.

Unexpected behaviour - fatal error: https://3v4l.org/BeFp5

Expected behaviour - warning: https://3v4l.org/ZhHrE (without
implementing an interface).

The introduction of an interface to the base class should not affect the behaviour of child classes.

Test script:
---------------
https://3v4l.org/BeFp5

Expected result:
----------------
Warning: Declaration of Bar::method() should be compatible with Foo::method($arg = NULL)

Actual result:
--------------
Fatal error: Declaration of Bar::method() must be compatible with Foo::method($arg = NULL)


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=76370&edit=1


Thread (7 messages)

« previous php.bugs (#216605) next »