php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75771 When using traits, class must be written prior to calling in same file.
Submitted: 2018-01-07 04:18 UTC Modified: -
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: sybrew at gmail dot com Assigned:
Status: Open Package: Class/Object related
PHP Version: Irrelevant OS: *
Private report: No CVE-ID: None
 [2018-01-07 04:18 UTC] sybrew at gmail dot com
Description:
------------
When a class is called from the same file it's written, you should be able to call them in any order.

However, when a class uses a trait, the order is strict.

Test script:
---------------
<?php

new A; // good.
new B; // fatal error.

class A {
	function __construct() {
		echo 'constructed A', "<br>\n";
	}
}

class B {
	use T;
	
	function __construct() {
		echo 'constructed B', "<br>\n";
		$this->traitfunc();
	}
}

new B; // good.

trait T {
	function traitfunc() {
		echo 'traitfunc called', "<br>\n";
	}
}

Expected result:
----------------
constructed A
constructed B
traitfunc called

Actual result:
--------------
constructed A

FATAL ERROR Class 'B' not found on line number 4

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-01-07 04:52 UTC] spam2 at rhsoft dot net
without opcache that's even not bound to traits
see https://bugs.php.net/bug.php?id=75542
 [2018-01-07 10:43 UTC] sybrew at gmail dot com
This issue is reproducible with Zend Opcache enabled.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 06 20:01:27 2024 UTC