Bug #69116 [NEW]: Strict mode error triggered not consistently

From: Date: Tue, 24 Feb 2015 16:48:16 +0000
Subject: Bug #69116 [NEW]: Strict mode error triggered not consistently
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-190933@lists.php.net to get a copy of this message
From:             m at klkvsk dot ru
Operating system: Linux
PHP version:      5.5.22
Package:          Class/Object related
Bug Type:         Bug
Bug description:Strict mode error triggered not consistently

Description:
------------
PHP does not trigger strict error "Declaration .. should be compatible
with .." when overriding method has incompatible arguments with base
method. But when either parent or child class implements any interface,
the error gets triggered. 
Example:
    error_reporting(-1);

    class A1 {
    	public function fn() {}
    }
    class B1 extends A1 {
    	public function fn($arg) {} // error is not triggered
    }

    interface I {}
    class A2 {
        public function fn() {}
    }
    class B2 extends A2 implements I {
        public function fn($arg) {} // error is triggered
    }


Another related bug that is probably a product of this bug, is when you
have set error handler with function that throws an exception on
E_STRICT, and then you put classes definition in try-catch, the child
class would be defined without its interfaces. 
Example: 
    set_error_handler(
        function ($code, $string) { throw new Exception($string, $code);
}, 
        E_STRICT
    );
    try {
        interface I1 {}
        interface I2 {}
        class A implements I1 { 
            public function fn() { echo 'foo'; } 
        }
        class B extends A implements I2 {
            public function fn($arg) { echo $arg; }
        }
    } catch (Exception $e) {
        // ignore
    }
    $b = new B();
    var_dump($b instanceof I1);         // outputs "bool(false)"
    var_dump($b instanceof I2);         // outputs "bool(false)"
    var_dump(class_implements('B'));    // outputs "array(1) { 'I1' =>
string(2) "I1" }"
    $b->fn('bar');                      // outputs "bar"

You can see that child class is defined and works, but it's interfaces
are lost. Only parent classes interfaces are kept. The real-world usage
of code like this might be implementation of an autoloader, where you
would put class definitions in separate files and include them here
inside try-catch. 

Test script:
---------------
<?php
error_reporting(-1);

class A1 {
	public function fn() {}
}
class B1 extends A1 {
	public function fn($arg) {}
}

interface I {}
class A2 {
	public function fn() {}
}
class B2 extends A2 implements I {
	public function fn($arg) {}
}


Expected result:
----------------
PHP Strict standards:  Declaration of B1::fn() should be compatible with
A1::fn() in /tmp/test.php on line 7
PHP Stack trace:
PHP   1. {main}() /tmp/test.php:0
PHP Strict standards:  Declaration of B2::fn() should be compatible with
A2::fn() in /tmp/test.php on line 15
PHP Stack trace:
PHP   1. {main}() /tmp/test.php:0


Actual result:
--------------
PHP Strict standards:  Declaration of B2::fn() should be compatible with
A2::fn() in /tmp/test.php on line 15
PHP Stack trace:
PHP   1. {main}() /tmp/test.php:0

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



Thread (3 messages)

« previous php.bugs (#190933) next »