Edit report at https://bugs.php.net/bug.php?id=55103&edit=1
ID: 55103
Comment by: clicky at erebot dot net
Reported by: imaggens at gmail dot com
Summary: Interfaces avoids Classes to exist
Status: Open
Type: Bug
Package: Class/Object related
Operating System: Windows 7
PHP Version: 5.3.6
Block user comment: N
Private report: N
New Comment:
PHP expects the parent classes/interfaces to have been defined before the class
that extends/implements them.
If I change the order of the lines from code sample #2 (eIUJWp56) to read:
<?php
namespace Test {
class Second {
public function getMessage() {
return 'Message from Second Class';
}
}
interface TestInterface {}
abstract class Zero implements TestInterface {
public function __construct() {
// Using a second class
$second = new Second;
print $second -> getMessage();
}
}
class First extends Zero {}
}
?>
Then PHP 5.3.3, PHP 5.3.8 & PHP 5.4.0RC2 happily accept the file (no error).
Previous Comments:
------------------------------------------------------------------------
[2011-08-24 22:27:45] imaggens at gmail dot com
Sorry, I don't know if it's allowed to do that, but is there any position about
this bug report?
------------------------------------------------------------------------
[2011-07-02 10:11:05] imaggens at gmail dot com
Sorry about the code, it was my mistake.
The correct code for Code Sample #2 is: http://codepad.org/eIUJWp56
And as requested, the code of other file:
<?php
require_once 'Test.php';
try {
$first = new Test\First;
} catch( Exception $e ) {
echo $e -> getMessage();
}
?>
I tried without the try...catch() and with it and, in both cases the error
remains.
------------------------------------------------------------------------
[2011-07-01 16:26:46] [email protected]
It seems to me your 2 code samples are identical, just one has output attached.
Where's the interface?
Please provide a *complete* sample, i.e. if your including one file make clear
what's in both files, the one having a call to "require" and the one being
required
------------------------------------------------------------------------
[2011-07-01 10:07:08] imaggens at gmail dot com
Description:
------------
First at all, one consideration about one of the informations provided in this
form is the PHP version. I'm not using 5.3.6. I'm using 5.3.3, which is not
listed. I f I chose "earlier", the form won't submit.
I can be wrong, but I think this bug is not fixed in newer versions, because
it's not a very common use.
The whole thing is, when interfaces and classes are in the same namespace AND
in same file, the 'implements' breaks the execution of the 'extends'. See Code
#1
As expected I can see "Message from Second Class", without quotes.
But if I add a interface (see Code #2) I get a Fatal Error: "Class 'Test\Zero'
not found", when it could be expected the same result as before.
But why this is important, if the best practices are to develop by following an
organized structure, with each class/interface in its own file?
The thing is, when DEVELOPING, this kind of organization is very useful, but if
the code produced during development stage is a little library, if all the
classes and interfaces are coded in one single file, only one call to
require_once is needed, and the code execution is three times faster than when
using an autoloader resource.
Note about CodePad's codes: I'd only saved the lines of code in this site, they
don't work from it, due PHP versions. But all the tests I made was in machine
with the configurations posted.
Test script:
---------------
[ Code #1 ]
http://codepad.org/pDOAiqBa
[ Code #2 ]
http://codepad.org/a42WgIT3
Expected result:
----------------
As said in Bug's Description, "Message from Second Class", witout quotes.
Actual result:
--------------
With the first code, I can see the expected result.
With the second code, as I said, I see a Fatal Error. If the stack traces
helps, here is it:
Fatal error: Class 'Test\Test\Zero' not found in C:\root\Test\Library.php on
line 5
Call Stack
# Time Memory Function Location
1 0.0004 326896 {main}( ) ..\index.php:0
2 0.0018 334024 require_once( 'C:\root\Test\Framework.php' )
..\index.php:3
Dump $_GET
Dump $_POST
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55103&edit=1