php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76539 Trait attribute is set incorrectly when using self::class with another string
Submitted: 2018-06-28 08:53 UTC Modified: 2020-03-02 20:42 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: christian dot lerch89 at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 7.1Git-2018-06-28 (Git) OS: Ubuntu 18.04
Private report: No CVE-ID: None
 [2018-06-28 08:53 UTC] christian dot lerch89 at gmail dot com
Description:
------------
When the value of a trait attribute is a concatenation of self::class (or __CLASS__) and another string, the value of the very first instantiation persists.

This problem seems to be introduced at 7.1 and still shows up on 7.2.4.

You may try the example with >=7.0 here: http://sandbox.onlinephpfunctions.com/code/cced66bdfaf2dc5d09a042e4de1947bd8198b95d

Test script:
---------------
trait MyTrait {
	protected $attr = self::class . 'Test';

	public function test() {
		return $this->attr;
	}
}

class A {
	use MyTrait;
}

class B {
	use MyTrait;
}

$a = new A();
$b = new B();

Expected result:
----------------
ATest
BTest

Actual result:
--------------
ATest
ATest

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-06-28 09:01 UTC] nikic@php.net
Looks like something in 7.3 fixed this as a side-effect: https://3v4l.org/dQ6hM
 [2018-07-04 17:51 UTC] kalle@php.net
-Status: Open +Status: Verified
 [2018-07-04 17:51 UTC] kalle@php.net
I added a test case for this in master to hopefully catch such a thing in the future, but the bug still remains open for 7.1 & 7.2
 [2018-07-09 17:27 UTC] cmb@php.net
> Looks like something in 7.3 fixed this as a side-effect: […]

FWIW, that was the IS_CONSTANT removal[1].

[1] <https://github.com/php/php-src/commit/ef5ea48741a1d227a10a6654c1a8cc2f02dd414f>
 [2020-03-02 20:42 UTC] nikic@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: nikic
 [2020-03-02 20:42 UTC] nikic@php.net
Closing as this is fixed per previous comments.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 05:01:32 2024 UTC