php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69467 Wrong checked for the interface by using Trait
Submitted: 2015-04-16 08:42 UTC Modified: 2015-04-17 09:04 UTC
From: zeli dot box at gmail dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 5.4.39 OS:
Private report: No CVE-ID: None
 [2015-04-16 08:42 UTC] zeli dot box at gmail dot com
Description:
------------
Create an interface in the class:
1. Adding trait to the class
2. Implement interface methods in the trait, but making them private/protected
3. Create class object
4. Checking the object for the instance of interface returns true

This bug in php version > 5.4.10

But versions less than 5.4.10 was a fatal error:
"Fatal error:  Access level to Foo::bad() must be public (as in class Baz)"

Test script:
---------------
interface Baz {
    public function bad();
}

trait Bar{
    protected function bad(){}
}

class Foo implements Baz{
    use Bar;
}

$test = new Foo();
var_dump($test instanceof Baz);

Expected result:
----------------
Fatal error

Actual result:
--------------
bool(true)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-04-16 09:18 UTC] requinix@php.net
-Status: Open +Status: Verified
 [2015-04-16 09:18 UTC] requinix@php.net
http://3v4l.org/p303Q

I can't bisect but the first place I would look for the change is
https://github.com/php/php-src/commit/3f8c729e693c432b438cd33679182260d8732e30
 [2015-04-17 09:04 UTC] laruence@php.net
seems we are entering the looser check path. 

diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 4e61f5f..39bdfec 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -3912,7 +3912,8 @@ static void zend_add_trait_method(zend_class_entry *ce, const char *name, const
 			}
 			zend_hash_quick_update(*overriden, arKey, nKeyLength, h, fn, sizeof(zend_function), (void**)&fn)
;
 			return;
-		} else if (existing_fn->common.fn_flags & ZEND_ACC_ABSTRACT) {
+		} else if (existing_fn->common.fn_flags & ZEND_ACC_ABSTRACT
+				&& !(existing_fn->common.scope->ce_flags & ZEND_ACC_INTERFACE)) {
 			/* Make sure the trait method is compatible with previosly declared abstract method */
 			if (!zend_traits_method_compatibility_check(fn, existing_fn TSRMLS_CC)) {
 				zend_error(E_COMPILE_ERROR, "Declaration of %s must be compatible with %s",


thanks
 [2015-04-21 14:41 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=c667c26f616f91db5b890d39807c77c4720ba507
Log: Fixed Bug #69467 (Wrong checked for the interface by using Trait)
 [2015-04-21 14:41 UTC] laruence@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 09:01:32 2024 UTC