|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #6418 Multiple class extends ordering error
Submitted: 2000-08-29 12:20 UTC Modified: 2007-01-07 11:26 UTC
Avg. Score:4.8 ± 0.4
Reproduced:11 of 12 (91.7%)
Same Version:1 (9.1%)
Same OS:5 (45.5%)
From: nathan at cjhunter dot com Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 4.0.1pl2 OS: Linux 2.0.36
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2000-08-29 12:20 UTC] nathan at cjhunter dot com
Multiple class extends creates ordering error

The below..
class c extends b {}

class b extends a {}

class a {}

gives an error 'Fatal error: Class c: Cannot inherit from undefined class b in test.html on line 4',

class b extends a {}

class a {}

// class c moved to bottom
class c extends b {}

works fine.

I imagine this could be fixed by first ordering the classes by their given dependencies and then process them one by one.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2000-08-31 08:48 UTC]
Yes, you cannot do this in PHP. In fact, you even shouldn't try doing this - why would you need to define class without knowing what it extends? I move it to feature request in the meantime, in case something will explain why this could be useful.
 [2002-09-09 09:04 UTC] scallop at iki dot fi
Yes, this is a feature that is definately needed in PHP.

I am currently working on a project which has very complex class hierarchy (dozens of classes extending each other). Due to complex class hierarchy and the huge amount of code, I am developing classes so that each class is in it's own file. And to make it all simple, I use a small script to combine all this classes into one huge file.

It is always possible to create a system that checks class dependencies and creates a combined classfile with classes in proper order.

However, I think this is a feature that _should_ be provided by php.

(tested on versions 4.2.3, 4.3.0 - still not implemented)

I hope I explained my self clear enough.

See my report (marked as duplicate):
 [2006-07-15 10:20 UTC] info at triin dot net
I had some quite hard time figuring out what is wrong with my code - this defenetly is a bug, not a feature request.

I had a lot of PHP files (class in each file) and at the beginning of each one there were the require_once statements to include all the other classes that were used by that class. And suddenly there was a weird collision...
 [2007-01-07 11:26 UTC]
You need to declare the class before you use it.
The other way around doesn't make sense and will not ever 
be supported.

With the "class per file" approach you can use autoload() 
or spl_autoload()
 [2010-06-03 02:50 UTC] tom at tomwardrop dot com
I don't know the internals of PHP, but maybe someone who does can respond to this. I agree with the comment by

"You need to declare the class before you use it.
The other way around doesn't make sense and will not ever 
be supported."

However, since when was declaring a class considered to be "using" a class? If possible, I propose that PHP check for the availability of extended classes and interfaces at the time an instance is created, or when the first static call is made, rather than when the class is declared. This is no doubt the most logical approach, but I have no idea whether this is technically possible to implement.

We can call this feature, just-in-time class checking :-)

I look forward to hearing a response from someone who knows the part of the PHP core that handles this.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Thu Nov 30 12:01:25 2023 UTC