Edit report at https://bugs.php.net/bug.php?id=55554&edit=1
ID: 55554 Comment by: g...@php.net Reported by: ryan at zuttonet dot com Summary: Trait methods overriding legacy constructors Status: Open Type: Bug Package: *Programming Data Structures Operating System: Ubuntu PHP Version: 5.4.0alpha3 Block user comment: N Private report: N New Comment: Hi Ryan: I am sorry, I don't think I understand what inconsistency you are pointing out. Could you elaborate on the problem? (And please include the code you are referring to directly here. Just to make it easier for me to know that we are talking about exactly the same code.) What I understand is you want to provide the constructor with a trait, right? Like this: <?php trait MyTrait { public function constructor() { echo "Foo\n"; } } class MyClass { use MyTrait { constructor as __construct; } } class MyClass2 { use MyTrait { constructor as MyClass2; } } echo "MyClass constructor: "; $o = new MyClass; // echos Foo echo "\nMyClass2 constructor: "; $o = new MyClass2; // doesn't echo Foo echo "\n"; ?> The problem I see here is that for MyClass2 the constructor does not actually get registered as a constructor but just as a normal method. That seems to be an inconsistency that needs to be fixed. Ok, now to the new vs. legacy constructors: class Bar { function Bar() { echo "BarBar new ctor\n"; } function __construct() { echo "Bar new ctor\n"; } } $o = new Bar; ?> Switching the order of the definition of the constructors doesn't influence the result, __construct always wins. Both your examples behaves identical to the situation if the __construct would have been defined directly in the class. So, where is the problem here? It is not an inconsistency with how PHP behaves without traits, from what I can see. Ah, and please try the latest code in the SVN, I am not exactly sure whether I changed anything that could be related to that between alpha3 and now. But I doubt it. Thanks Stefan Previous Comments: ------------------------------------------------------------------------ [2011-08-31 15:52:43] ryan at zuttonet dot com Apologies; the literal expected output for the provided test scripts should be: "this is a constructor" As this is the output when using __construct() in the class definition instead of a legacy-style constructor. ------------------------------------------------------------------------ [2011-08-31 15:49:30] ryan at zuttonet dot com Description: ------------ For the sake of consistency, exactly one of the following should be implemented: 1. Trait methods should be able to override __construct definitions 2. Trait methods should not be able to override legacy constructor definitions Currently, trait methods are not able to override __construct definitions. Trait methods are able to override legacy constructor definitions. Test script: --------------- Here are two test cases that will reproduce the defect: https://gist.github.com/1183844 Expected result: ---------------- A trait-level __construct method (or a trait-level method aliased as __construct) should not be able to override any type of constructor in a class Actual result: -------------- Fatal error: Call to private SomeClass::__construct() from invalid context ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=55554&edit=1