|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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. Patchesbug62537 (last revision 2017-02-07 23:23 UTC by mail at pmmaga dot net)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 13:00:01 2025 UTC |
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.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