Edit report at https://bugs.php.net/bug.php?id=63646&edit=1
ID: 63646
User updated by: phpbugs at catchall dot drarok dot com
Reported by: phpbugs at catchall dot drarok dot com
Summary: Trait method names matching classes that use said
trait treated as constructors
Status: Wont fix
Type: Bug
Package: Scripting Engine problem
Operating System: OS X 10.8
PHP Version: master-Git-2012-11-29 (Git)
Block user comment: N
Private report: N
New Comment:
@nikic: precisely what I was thinking!
Previous Comments:
------------------------------------------------------------------------
[2012-12-01 12:19:10] [email protected]
@laruence: On the other hand, if you are using a namespaced class old-style
constructors aren't used either. So the same might also be done with traits.
------------------------------------------------------------------------
[2012-12-01 11:31:06] [email protected]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php
the method name same as class name is considered as constructor as of PHP 5.4
although we encourage use __construct instead of that. but it should also be
compatible
------------------------------------------------------------------------
[2012-11-29 09:48:49] phpbugs at catchall dot drarok dot com
Description:
------------
If you use a Trait which has a method name matching your class name, it is used
as
an old-style constructor in the case where the using class doesn't define a
constructor.
If you *do* define a constructor, you receive the error "Fatal error: Hello has
colliding constructor definitions coming from traits".
Test script:
---------------
<?php
trait HelloWorld {
public function hello() { echo 'Hello, World!', PHP_EOL; }
static public function world() { echo 'World; Hello!', PHP_EOL; }
}
class Hello { use HelloWorld; }
class World { use HelloWorld; }
new Hello();
new World();
/*
* # php example.php
* Hello, World!
* World; Hello!
*/
Expected result:
----------------
Given that Traits cannot be used in legacy code, I'd expect the method name to
be
irrelevant to constructor behaviour.
Actual result:
--------------
Method names that match the class name are used as a constructor.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63646&edit=1