class not found when extending class that implements interface in PHP

后端 未结 2 1944
情深已故
情深已故 2020-12-21 05:29

the code above throws \"Fatal error: Class \'B\' not found\"... Is it a php

相关标签:
2条回答
  • 2020-12-21 05:55

    You are declaring the classes and interfaces in the wrong order. This is correct:

    interface C {}
    
    class B implements C {}
    
    class A extends B {}
    
    0 讨论(0)
  • 2020-12-21 05:59

    You have trouble specifically with the order of your class definitions. As long as the interface is defined in the same file, it can be declared anywhere - but classes must be defined before they can be extended.

    The following is perfectly valid order in PHP:

    class B implements C { ... }
    class A extends B { ... }
    interface C { ... }
    

    There is a closed bug requesting clarification in the PHP5 docs.

    An answer to a similar question (Does the order of class definition matter in PHP?) mentions Autoloading. You may want to look into this if you're using multiple files.

    0 讨论(0)
提交回复
热议问题