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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: sybrew at gmail dot com
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2020 The PHP Group
All rights reserved.
Last updated: Wed Sep 30 07:01:23 2020 UTC