Today I noticed the strangest bug in PHP. Apparently there's a problem with circular dependencies and interfaces.
This is how the problem occurs:
- <?php
-
- // this is class1.php
-
- require_once 'iclass.php';
-
- class class1 implements iclass {
-
- }
-
- ?>
- <?php
-
- // this is class2.php
-
- require_once 'class1.php';
-
- class class2 extends class1 {
-
- }
-
- ?>
- <?php
-
- // this is iclass.php
-
- require_once 'class2.php';
-
- interface iclass {
-
- function setSomething(class2 $object);
-
- }
-
- ?>
Now, if you run class1.php, you will get the following error:
- Fatal error: Class 'class1' not found in /home/filemobile/testscript/class2.php on line 5
I know this is not proper OOP, but its still a strange error. Normally PHP is pretty good at these weird structures ;). Weirdest thing was that it happened for me on one server, but not the other.
There is a bug marked bogus for this, which is understandable. It would be great though if the PHP could somehow throw an error thats a bit more helpful =P. Its a strange error, especially since I could only reproduce it with this structure, and not with for example just 2 classes with circular dependencies.
