Bug #77470 [Ver->Wfx]: Methods in traits can unwillingly function as constructor
| From: | nikic@php.net | Date: | Wed, 30 Jan 2019 08:31:27 +0000 |
| Subject: | Bug #77470 [Ver->Wfx]: Methods in traits can unwillingly function as constructor | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-219284@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77470&edit=1
ID: 77470
Updated by: nikic@php.net
Reported by: bas at dmt-software dot nl
Summary: Methods in traits can unwillingly function as
constructor
-Status: Verified
+Status: Wont fix
Type: Bug
Package: Class/Object related
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Marking this as won't fix per the above comment. Legacy dtor support is gone from PHP 8 and the
code behaves as desired there. I don't think it's worthwhile to make a (BC-breaking)
change in 7.4 in addition to that.
Previous Comments:
------------------------------------------------------------------------
[2019-01-25 10:36:43] nikic@php.net
While trying to fix this I found bug #55554, which is where the current behavior was implemented.
It also has this example, which makes slightly more sense in that the legacy ctor name is used as an
explicit alias:
trait TConstructor {
public function constructor() {
echo "ctor executed\n";
}
}
class NewConstructor {
use TConstructor {
constructor as __construct;
}
}
class LegacyConstructor {
use TConstructor {
constructor as LegacyConstructor;
}
}
Given this, I'm not sure whether to make this change. It would only be able to go into 7.4 if
at all, and in PHP 8 legacy constructors are going away entirely, so the problem will resolve itself
at that time.
------------------------------------------------------------------------
[2019-01-16 15:52:21] nikic@php.net
I agree that we should only treat __construct as a constructor when coming from a trait.
Interesting that this only comes up now, probably not many people using traits with non-namespaced
code.
------------------------------------------------------------------------
[2019-01-16 15:44:01] bas at dmt-software dot nl
Description:
------------
A class without __construct method cannot use a trait that has a method that equals the class
it's name. It will execute the trait method as constructor when E_DEPRECATED errors are
ignored. This should never happen.
Test script:
---------------
error_reporting(E_ALL ^ E_DEPRECATED);
class Callback {
use CallbackTrait;
}
trait CallbackTrait {
public function callback() {
echo 'this is called';
}
}
new Callback();
---
https://3v4l.org/CWBai
Expected result:
----------------
It should ignore the method as constructor like extends does or it should trigger a fatal error as
it does when a constructor is present (https://3v4l.org/nbh6X)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77470&edit=1