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: -
From: sybrew at gmail dot com Assigned:
Status: Open Package: Class/Object related
PHP Version: Irrelevant OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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-2019 The PHP Group
All rights reserved.
Last updated: Mon Feb 18 14:01:25 2019 UTC