php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39567 Cannot find Class definition when there is one
Submitted: 2006-11-21 09:55 UTC Modified: 2006-11-21 12:11 UTC
From: imkow at 163 dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.0 OS: WinXp sp2+Apache2.2+Mod_PHP
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: imkow at 163 dot com
New email:
PHP Version: OS:

 

 [2006-11-21 09:55 UTC] imkow at 163 dot com
Description:
------------
Two files:
file1.php:
class A{}

file2.php?F
require("file1.php");
$obj=new B;
class B extends A{}

when runing file2.php, it throws a Fatal Error, saying Class B can't be found in the line where "$obj=new B;" is.

But, in following code, things will just be fine:
class A{}
$obj=new B;
class B extends A{}


I use an official release of PHP 5.2.0 under windows. All mods are also from Pecl.



Reproduce code:
---------------
//file1.php:

class A{}



//file2.php?F
require("file1.php");
$obj=new B;
class B extends A{}

Expected result:
----------------
nothing..

Actual result:
--------------
( ! ) Fatal error: Class 'B' not found in file2.php on line 2

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-11-21 10:16 UTC] tony2001@php.net
Please change require("file1.php"); to require("/absolute/path/to/file1.php"); and see if it works.
 [2006-11-21 11:21 UTC] imkow at 163 dot com
i have changed my code to absolute path.
require("F:/LXH/Webs/pware/file2.php");
$k= new B;
class B extends A{}

the problem is still there. a fatal error was thrown out.
 [2006-11-21 11:26 UTC] imkow at 163 dot com
require("F:/LXH/Webs/pware/file1.php");
a correction, it was file1.php, not file2.php
but im sure the problem is still there.
 [2006-11-21 11:33 UTC] tony2001@php.net
Expected behaviour.
Class "B" cannot be declared in compile time, because class "A" is declared in another file.
But in runtime, when you execute the line "$obj=new B;", the class "B" doesn't exist yet.
 [2006-11-21 11:34 UTC] imkow at 163 dot com
require("file1.php");
$j= new A;
$k= new B;
class B extends A{}

I've changed the code to ensure class A is defined.
as shown above, only the line where $k in throws error..
 [2006-11-21 11:42 UTC] tony2001@php.net
Please read what I said in my previous comment.
 [2006-11-21 11:44 UTC] imkow at 163 dot com
do you mean that it's forbidden to write code like
within one file:
class A{}
$j=new B;
class B extends A{}

or code like:

$j=new B;
class B{}

is also forbidden.

But at this moment they all work fine in php. if those mentioned are forbidden, maybe we should write something in the manual to clarify this...
 [2006-11-21 11:49 UTC] tony2001@php.net
No, I didn't mean this.
I meant exactly what I said - there is no way to declare this class in compile time, so it's declared in runtime.
And you get the error because you're trying to use the class which is not yet declared.
Please keep this report a bogus. Thank you.
 [2006-11-21 12:11 UTC] imkow at 163 dot com
All right. but you people have to notice this is still a design fault that should be addressed in somewhere. we php script developers don't know much about php's internal structure. we can hardly distinguish what is done in compile time and what is not. The problem isn't on the manual where you can learn. So people will be used to write code like this until something be pointed out or changed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 18:01:28 2024 UTC