扩展在PHP中实现接口的类时找不到该类 [英] class not found when extending class that implements interface in PHP
问题描述
<?php
class A extends B {}
class B implements C {}
interface C {}
上面的代码抛出致命错误:找不到类'B'" ... 这是一个PHP错误?还是?
环境:带有Suhosin-Patch(cli)的PHP 5.3.6-13ubuntu3.2(内置:2011年10月13日23:19:13) 版权所有(c)1997-2011 The PHP Group Zend Engine v2.3.0,版权所有(c)1998-2011 Zend Technologies 与Xdebug v2.1.0,版权所有(c)2002-2010,作者Derick Rethans "
您在类定义的顺序上遇到了麻烦.只要在同一文件中定义了接口,就可以在任何地方声明它-但是必须在扩展类之前定义类.
以下是在PHP中完全有效的命令:
class B implements C { ... }
class A extends B { ... }
interface C { ... }
一个类似问题的答案( PHP中的类定义问题?)提到自动加载.如果您使用多个文件,则可能需要调查一下.
<?php
class A extends B {}
class B implements C {}
interface C {}
the code above throws "Fatal error: Class 'B' not found"... Is it a php bug? Or?
Environment: "PHP 5.3.6-13ubuntu3.2 with Suhosin-Patch (cli) (built: Oct 13 2011 23:19:13) Copyright (c) 1997-2011 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans "
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.
这篇关于扩展在PHP中实现接口的类时找不到该类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!