Doc #62936 [Opn->Csd]: Last Trait wins on importing conflicting properties
| From: | cmb@php.net | Date: | Sat, 07 Jul 2018 17:12:40 +0000 |
| Subject: | Doc #62936 [Opn->Csd]: Last Trait wins on importing conflicting properties | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-15853@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62936&edit=1
ID: 62936
Updated by: cmb@php.net
Reported by: r dot wilczek at web-appz dot de
Summary: Last Trait wins on importing conflicting properties
-Status: Open
+Status: Closed
Type: Documentation Problem
Package: Class/Object related
PHP Version: 5.4.6
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
This ticket appears to be obsolete as of PHP 7.0.0, see
<https://3v4l.org/uJA3s>.
Previous Comments:
------------------------------------------------------------------------
[2012-11-11 11:42:33] cameron at datashovel dot com
This seems to be related, and appears to show some sort of inconsistency.
FreeBSD www 9.1-PRERELEASE FreeBSD 9.1-PRERELEASE #3:
root@www:/usr/obj/usr/src/sys/XENHVM amd64
PHP 5.4.6 (cli) (built: Sep 11 2012 09:19:59)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
CODE:
---------
$classFoo = new \ReflectionProperty('MyClass','foo');
$traitFoo = new \ReflectionProperty('MyTrait','foo');
echo $traitFoo->getDeclaringClass()->getName().PHP_EOL;
echo $traitFoo->getDocComment().PHP_EOL;
echo $classFoo->getDeclaringClass()->getName().PHP_EOL;
echo $classFoo->getDocComment().PHP_EOL;
OUTPUT:
--------
MyTrait
/**
* Comes from trait
*/
MyClass
/**
* Comes from trait
*/
------------------------------------------------------------------------
[2012-08-26 16:26:57] r dot wilczek at web-appz dot de
Actually, the docblock is taken from the last trait listed in the use-statement:
class Foo
{
use TraitA, TraitB;
private $foo;
}
If TraitB provides private $foo, the docblock will be taken from TraitB, discarding any private $foo
defined in TraitA or class Foo.
class Foo
{
use Trait, TraitA;
private $foo;
}
Now the docblock is taken from TraitA.
------------------------------------------------------------------------
[2012-08-26 16:02:04] r dot wilczek at web-appz dot de
Description:
------------
In addition to the PHP manual stating ...
If a trait defines a property then a class can not define a property with the same name,
otherwise an error is issued. It is an E_STRICT if the class definition is compatible (same
visibility and initial value) or fatal error otherwise
... note, that if you ignore the E_STRICT-notice, PHP will fetch the docblock of the imported
property, discarding the docblock of conflicting property in the importing module.
Test script:
---------------
error_reporting(E_ALL ^ E_STRICT);
trait MyTrait
{
/**
* Comes from trait
*/
private $foo;
}
class MyClass
{
use MyTrait;
/**
* Comes from class
*/
private $foo;
}
$classFoo = new \ReflectionProperty('MyClass', 'foo');
$traitFoo = new \ReflectionProperty('MyTrait', 'foo');
var_export($traitFoo->getDocComment() == $classFoo->getDocComment());
Expected result:
----------------
true // if the composed class contains the docblock of MyTrait::$foo
false // if the composed class contains the docblock of MyClass::$foo
Actual result:
--------------
true
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62936&edit=1