php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53508 Class not found caused by require_once
Submitted: 2010-12-09 15:47 UTC Modified: 2015-02-25 19:55 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: exsystemchina at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.3 OS: Mac OS X 10.6
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: exsystemchina at gmail dot com
New email:
PHP Version: OS:

 

 [2010-12-09 15:47 UTC] exsystemchina at gmail dot com
Description:
------------
A weird bug, appears only when the script contains require_once:
A class is defined with an interface implemented, but the PHP interpreter produces 
a fatal error saying class not defined. But everything goes well if the class 
doesn't implemented an interface.

Test script:
---------------
Download this file, and unzip it:
http://files.cnblogs.com/ExSystem/FrameworkDSW.zip
Make sure your php include_path contains the parent directory of FrameworkDSW, and to set error reporting level to E_ALL in case.
Run FrameworkDSW\System.php, and get the fatal error.
****And only the classes defined before the declaration of TObject inside System.php are declared succesfully(I guess it by invoking get_declared_classes()).

Then modify the line 285 of System.php to
class TObject {
rather than
class TObject implements IInterface {
and run it again, to see the difference.
****And all the classes inside System.php are declared successfully.

Expected result:
----------------
<Nothing>

Actual result:
--------------
<br />
<b>Fatal error</b>:  Class 'TObject' not found in 
<b>{your_include_path}\FrameworkDSW\Utilities.php</b> on line <b>81</b><br />

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-10 13:39 UTC] exsystemchina at gmail dot com
I found that if I put require_once at the end of the php file, the fatal error 
won't appear any more... just inspired by another bug report.
 [2010-12-13 10:18 UTC] aharvey@php.net
-Status: Open +Status: Feedback
 [2010-12-13 10:18 UTC] aharvey@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.


 [2010-12-14 13:14 UTC] exsystemchina at gmail dot com
-Status: Feedback +Status: Open
 [2010-12-14 13:14 UTC] exsystemchina at gmail dot com
ok, i have just know that a login is needed to download the zip file.....
now i have shorten the buggy code.
 [2010-12-14 13:17 UTC] exsystemchina at gmail dot com
//System.php
<?php
require_once 'Utilities.php';

class TJustTest {
    public function __construct() {
    		var_dump(get_declared_classes());
    }
}

interface IInterface {
    public function Supports($AInterface);
}

class TObject implements IInterface {
		public function Supports($AInterface) {
				echo 'hi';
		}
}

class TJustAnotherTest {
		public function Foo() {
				echo 'foo';
		}
}
?>

//Utilities.php
<?php

require_once 'System.php';

class TSomethingBoring {
		public function Boring() {
				echo 'really boring';
		}
}

$a=new TJustTest();

class TMustBeWrong extends TObject {
		function Tragedy() {
				echo 'this will not be seen.';
		}
}
?>



Just run System.php to see the fatal error with other classes before TObject loaded successfully 
inside System.php
 [2010-12-20 03:34 UTC] kalle@php.net
-Status: Open +Status: Feedback
 [2010-12-20 03:34 UTC] kalle@php.net
You don't happen to have any opcode cachers like APC installed?
 [2010-12-20 03:55 UTC] exsystemchina at gmail dot com
-Status: Feedback +Status: Open
 [2010-12-20 03:55 UTC] exsystemchina at gmail dot com
no cacher is installed.
 [2010-12-22 06:47 UTC] exsystemchina at gmail dot com
and something important i forgot to mention:
THIS WILL ONLY HAPPEN WHEN RUNNING THE SCRIPT, everything's all right when 
debugging.
 [2012-02-26 08:19 UTC] stas@php.net
-Status: Open +Status: Not a bug
 [2012-02-26 08:19 UTC] stas@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

TObject is not yet defined at the time TMustBeWrong line is executed, since 
require is executed before TObject definition is executed. I would suggest 
cleaning up the code and getting includes (requires) in proper order.
 [2015-02-25 16:44 UTC] sky at stebnicki dot net
How is this not a bug? I am experiencing the same thing and it very much looks like a bug to me.
 [2015-02-25 19:54 UTC] requinix@php.net
@sky:
As stas said three years ago, the require() will execute Utilities.php as the first thing, except that file needs something System.php defined, except that hasn't happened yet because PHP is still doing the require. The execution is out of order.
The dependency is that Utilities.php needs System.php, therefore Utilities.php must require() System.php. Not the other way around.
 [2015-02-25 19:55 UTC] requinix@php.net
*require_once()
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Apr 25 11:01:29 2025 UTC