php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #62537 Composed class has fatal error with duplicate, equal array properties
Submitted: 2012-07-11 19:45 UTC Modified: 2017-02-07 23:45 UTC
Votes:15
Avg. Score:4.4 ± 1.1
Reproduced:12 of 14 (85.7%)
Same Version:3 (25.0%)
Same OS:5 (41.7%)
From: jeremeamia at gmail dot com Assigned:
Status: Verified Package: Class/Object related
PHP Version: Irrelevant OS: *
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2012-07-11 19:45 UTC] jeremeamia at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/language.oop5.traits
---

The manual says, "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."

This does not appear to be true when the same-named properties have array values. 
See the test script for a quick example. It seems that it behaves as the 
documentation describes only with scalar values.

This is either missing/erroneous documentation (i.e. the behavior is intended), or 
it is a bug (i.e. equal array values should not cause a fatal error).

Test script:
---------------
trait Foo {public $var = [];}
trait Bar {public $var = [];}
class Baz {use Foo, Bar;}

Expected result:
----------------
To behave as the documentation describes, i.e. no fatal errors occuring.

Actual result:
--------------
PHP Fatal error:  Foo and Bar define the same property ($var) in the composition 
of Baz. However, the definition differs and is considered incompatible.

Patches

bug62537 (last revision 2017-02-07 23:23 UTC) by mail at pmmaga dot net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-07-12 01:36 UTC] jeremeamia at gmail dot com
Another test script, which might be even more relevant:

  trait Foo {public $var = [];}
  class Bar {use Foo; public $var = [];}

This also causes a fatal error for me, which should only be a strict error based 
on the documentation.
 [2012-07-20 09:07 UTC] aharvey@php.net
I'd call this a bug, rather than a documentation issue; if Foo::$var === Bar::$var, I can't really see why it should fatal.

Reclassifying, and we'll see what the engine folk think.
 [2012-07-20 09:07 UTC] aharvey@php.net
-Type: Documentation Problem +Type: Bug -Package: Documentation problem +Package: Class/Object related
 [2012-10-26 19:09 UTC] dagguh at gmail dot com
If it is logically the same thing, it should be only in one of these traits, don't 
you think?
SRP and DRY, fellas
 [2013-09-15 01:27 UTC] metamarkers at gmail dot com
This is a bug. It should not a be a fatal error if the initial value is even the 
same **type**. Consider using traits to define default static vars. Defining them 
through inheritance, these static vars are clobbered unless explicitly redefined 
in every child class.

It makes perfect sense not to allow this with constants, but why static 
variables?
 [2013-09-19 09:10 UTC] gron@php.net
-Status: Open +Status: Verified
 [2013-09-19 09:10 UTC] gron@php.net
I believe this is going to be problematic, because the object/array is not the 
same, i.e., it is not the same literal, and I assume that I currently check for 
object identity. Object identity works for most literals, but not arrays.

To fix this, special care is required. A value comparison might be possible, and 
would be necessary here. A simple check for the type is not sufficient, because 
$var = 1; vs $var = 2; should result in the expected error.

But yes, in general, I consider this behavior a bug.
 [2017-02-07 23:09 UTC] mail at pmmaga dot net
In PHP7.0, this E_STRICT was dropped as described in the 7.0 docs: http://php.net/manual/en/migration70.incompatible.php

> Same (compatible) property in two used traits	- Notice removed, triggers no error

However, the documentation on traits still mentions the E_STRICT.
 [2017-02-07 23:45 UTC] requinix@php.net
-Operating System: Ubuntu +Operating System: * -PHP Version: 5.4.4 +PHP Version: Irrelevant
 [2017-02-07 23:45 UTC] requinix@php.net
The documentation is indeed out of date.

No E_STRICT in PHP 7:
- https://3v4l.org/Jafoi matching in two traits
- https://3v4l.org/pQYTl matching in traits and class

Conflicting definitions:
- https://3v4l.org/brZt0 conflict between traits
- https://3v4l.org/umfVE conflict between trait and class
 [2017-02-07 23:45 UTC] requinix@php.net
-Type: Bug +Type: Documentation Problem
 [2017-02-09 17:40 UTC] mail at pmmaga dot net
I've included a patch for the documentation on this ticket as well.
 [2017-03-13 18:34 UTC] tularis@php.net
Automatic comment from SVN on behalf of tularis
Revision: http://svn.php.net/viewvc/?view=revision&revision=342111
Log: Documents the issue described in bug 62537.

--
Provided by Pedro Magalhães (mail@pmmaga.net)
 [2017-07-13 13:43 UTC] walle dot sthlm at gmail dot com
The following snippet results in the "definition differs and is considered incompatible" fatal error in 7.0+. Can this be considered as a bug? 

const VALUE = true;

trait Foo {public $var = VALUE;}
trait Bar {public $var = VALUE;}
class Baz {use Foo, Bar;}

https://3v4l.org/kkHFe
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC