php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55103 Interfaces avoids Classes to exist
Submitted: 2011-07-01 10:07 UTC Modified: 2017-01-02 14:33 UTC
Votes:3
Avg. Score:3.7 ± 0.9
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: imaggens at gmail dot com Assigned:
Status: Duplicate Package: Class/Object related
PHP Version: 5.3.6 OS: Windows 7
Private report: No CVE-ID: None
 [2011-07-01 10:07 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-01 16:26 UTC] fa@php.net
-Status: Open +Status: Feedback
 [2011-07-01 16:26 UTC] fa@php.net
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-02 10:11 UTC] 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-04 08:47 UTC] aharvey@php.net
-Status: Feedback +Status: Open
 [2011-08-24 22:27 UTC] 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-12-04 09:52 UTC] clicky at erebot dot net
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).
 [2011-12-05 17:56 UTC] imaggens at gmail dot com
That's weird, but solves the problem.

So remains the suggestion to not require this specific order to work. It would be 
helpful so we could simplify a "compiler" for each module of a system we have.
 [2017-01-02 14:33 UTC] nikic@php.net
-Status: Open +Status: Duplicate
 [2017-01-02 14:33 UTC] nikic@php.net
Duplicate of bug #54708.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Jul 11 20:01:24 2020 UTC