php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #62936 Last Trait wins on importing conflicting properties
Submitted: 2012-08-26 16:02 UTC Modified: 2018-07-07 17:12 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: r dot wilczek at web-appz dot de Assigned: cmb (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.4.6 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: r dot wilczek at web-appz dot de
New email:
PHP Version: OS:

 

 [2012-08-26 16:02 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-26 16:26 UTC] 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:26 UTC] r dot wilczek at web-appz dot de
-Summary: Trait-Properties overwrite properties in the importing scope +Summary: Last Trait wins on importing conflicting properties
 [2012-11-11 11:42 UTC] 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
     */
 [2017-01-28 12:15 UTC] cmb@php.net
-Package: Documentation problem +Package: Class/Object related
 [2018-07-07 17:12 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2018-07-07 17:12 UTC] cmb@php.net
This ticket appears to be obsolete as of PHP 7.0.0, see
<https://3v4l.org/uJA3s>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC