[PHP-BUG] Req #78807 [NEW]: Method signature types strictly enforced - should allow subclasses

[email protected] ("mlambley at gmail dot com") Wed, 13 Nov 2019 03:31:44 +0000
Newsgroups php.standards
Message-ID <[email protected]>
From:             mlambley at gmail dot com
Operating system: All
PHP version:      7.3.11
Package:          PHP Language Specification
Bug Type:         Feature/Change Request
Bug description:Method signature types strictly enforced - should allow subclasses

Description:
------------
Not sure if this is a bug or a feature request, but the example code I
have posted produces a warning:
Warning:  Declaration of SubA::fn(SubB $b) should be compatible with
A::fn(B $b)

SubB is a subclass of B, so the method override in class SubA should be
valid. I would expect the behaviour to be similar to how instanceof
works: 
var_dump((new SubB()) instanceof B); //true
That line of code returns true, and it should, because of OOP.

I understand that warnings are not errors, but when I'm publishing
modules which may be used by the general public it is impractical to
tell them that they must disable warnings.

Is it possible for PHP to respect subclasses in typed method signatures,
or will this have unforeseen behaviours I haven't anticipated?

Thank you.

Test script:
---------------
<?php
class A {
    public function fn(B $b) {}
}

class SubA extends A {
    public function fn(SubB $b) {
        parent::fn($b);
    }
}

class B {}
class SubB extends B {}

$obj = new SubA();
$obj->fn(new SubB());


-- 
Edit bug report at https://bugs.php.net/bug.php?id=78807&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=78807&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=78807&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=78807&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=78807&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=78807&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=78807&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=78807&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=78807&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=78807&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=78807&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=78807&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=78807&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=78807&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=78807&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=78807&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=78807&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=78807&r=mysqlcfg