Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

class not found when extending class that implements interface in 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 "

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...