Bug #69612 [NEW]: incorrect strict warnings for type hints in method

From: Date: Sat, 09 May 2015 11:45:09 +0000
Subject: Bug #69612 [NEW]: incorrect strict warnings for type hints in method
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-192600@lists.php.net to get a copy of this message
From:             rhymoid at gmail dot com
Operating system: 
PHP version:      5.5.24
Package:          Compile Warning
Bug Type:         Bug
Bug description:incorrect strict warnings for type hints in method

Description:
------------
An overriding method in a subclass is technically still compatible if

* it gained a default value (or = null in case of class type hints),
or
* a class type hint is changed into an ancestor class, or
* a type hint is removed.

These changes do not cause fatal errors in PHP 5.5, but do cause "Strict
standards" warnings for the latter two. I believe this is incorrect: no
warnings should be issued.

This was hinted at in a comment from 2012-02-11 on bug #46851.


Test script:
---------------
<?php

class Fruit {}
class Banana extends Fruit {}
class Lemon extends Fruit {}

class Base
{
    public function methodOne(Banana $x) {}
    public function methodTwo(array $x) {}
}

class OverrideNullable extends Base
{
    public function methodOne(Banana $x = null) {}
    public function methodTwo(array $x = null) {}
}

class OverrideParent extends Base
{
    public function methodOne(Fruit $x) {}
}

class OverrideWildcard extends Base
{
    public function methodOne($x) {}
    public function methodTwo($x) {}
}

(new Base())->methodOne(new Banana());
(new Base())->methodTwo([]);

(new OverrideNullable())->methodOne();
(new OverrideNullable())->methodTwo();

(new OverrideParent())->methodOne(new Lemon());

(new OverrideWildcard())->methodOne('');
(new OverrideWildcard())->methodTwo('');

echo "Output intentionally left blank." , PHP_EOL;


Expected result:
----------------
Output intentionally left blank.


Actual result:
--------------
PHP Strict Standards:  Declaration of OverrideParent::methodOne() should
be compatible with Base::methodOne(Banana $x) in /private/tmp/test.php
on line 22

Strict Standards: Declaration of OverrideParent::methodOne() should be
compatible with Base::methodOne(Banana $x) in /private/tmp/test.php on
line 22
PHP Strict Standards:  Declaration of OverrideWildcard::methodOne()
should be compatible with Base::methodOne(Banana $x) in
/private/tmp/test.php on line 28

Strict Standards: Declaration of OverrideWildcard::methodOne() should be
compatible with Base::methodOne(Banana $x) in /private/tmp/test.php on
line 28
PHP Strict Standards:  Declaration of OverrideWildcard::methodTwo()
should be compatible with Base::methodTwo(array $x) in
/private/tmp/test.php on line 28

Strict Standards: Declaration of OverrideWildcard::methodTwo() should be
compatible with Base::methodTwo(array $x) in /private/tmp/test.php on
line 28
Output intentionally left blank.


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



Thread (5 messages)

« previous php.bugs (#192600) next »