[PHP-BUG] Bug #74394 [NEW]: incosistent: Declaration of B::test(string $a) should be compatible

[email protected] ("spam2 at rhsoft dot net") Sat, 8 Apr 2017 21:18:18 GMT
Newsgroups php.standards
Message-ID <[email protected]>
From:             spam2 at rhsoft dot net
Operating system: Linux
PHP version:      7.1.4RC1
Package:          PHP Language Specification
Bug Type:         Bug
Bug description:incosistent: Declaration of B::test(string $a) should be compatible

Description:
------------
Warning: Declaration of B::test(string $a) should be compatible with
A::test($a)

Fatal error: Declaration of B::test($a) must be compatible with
A::test($a): string in /mnt/data/downloads/test.php on line 17
___________________________________________

case 1: Warning - that can be handeled

<?php declare(strict_types=1);
class A
{
 public function test($a)
 {

 }
}
class B extends A
{
 public function test(string $a)
 {

 }
}
?>
___________________________________________

case 2: introduce a return type in the extended class is even possible

<?php declare(strict_types=1);
$x = new B();
class A
{
 public function test($a)
 {

 }
}

class B extends A
{
 public function test($a): string
 {

 }
}
?>
___________________________________________

case 3: add return types in the base class breaks any code which extends
it and that makes it just impossible to introduce return-types on a
larger code base becaus eyou would need to *first* add the return types
to every extended class and only after that is done you can add it to
the shared library providing the base class - in case of scalar
type-hints you just need to "tail -f" on the error logs and fix the
warnings while all sites are online and working

<?php declare(strict_types=1);
$x = new B();
class A
{
 public function test($a): string
 {

 }
}

class B extends A
{
 public function test($a)
 {

 }
}
?>

Expected result:
----------------
only a warning when the base class defines a return type and the extend
class not to have a way fix the dfinitions in all code wich extends the
base class while the pages ar enot broken

Actual result:
--------------
impossible to introduce return types on classes which are extended
without touch *before* any extending code - that is not realistic in
case of hundrets of virtual hosts and more important makes it impossible
for anybody who publishes php-classes to add return-types without
completly break users code

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