test.inc.php(包含文件):<?php
class base{
}
var_dump(new tester());
?>
test.php(入口文件):
<?php
require_once('test.inc.php');
class tester extends base{
}
?>只要将extends base 去掉就可以得到正确的var_dump结果,反之,将出现fatal error:找不到tester类。
只知道现象,求解!
thx!

解决方案 »

  1.   

    var_dump(new tester());
    为什么写在test.inc.php文件中?
    在test.inc.php中还没有tester类,就执行这句当然会有问题。
      

  2.   

    楼上,您可以将extends base去掉,再运行一下试试,应该可以正确运行。php5以后(猜测),.php文件先解析(汇编)再执行的,类放在初始化语句的前面和后面都无太大的关系。
      

  3.   

    PHP对于class的定义有严格的顺序要求,下面是PHP帮助文件中"extends"部分的注意点:Note: Classes must be defined before they are used! If you want the class Named_Cart to extend the class Cart, you will have to define the class Cart first. If you want to create another class called Yellow_named_cart based on the class Named_Cart you have to define Named_Cart first. To make it short: the order in which the classes are defined is important. 
      

  4.   

    test.php中tester是base的扩展,所以var_dump(new tester());必须放在tester的定义之后才能正确执行。
    base -> tester -> new tester 才是正确的。如果tester不是base的子类,那么执行顺序相当于tester -> new tester,就没有问题了。
      

  5.   

    哈哈.不错..学习好像只有extends 关键字要顺序的.
      

  6.   

    http://docs.php.net/manual/zh/language.oop5.basic.php  --- PHP文档中搜索到的,似乎说“先定义,后使用”;看看,php中含糊不清的地方还不少,呵呵==================================== ashraf dot samhouri at hotmail dot com
    24-May-2008 01:35
    @info -- 20-AprilThis is because you requested class "b" before defining it, not because you defined class "b" before "a". It doesn't make a difference which class you define first.
    info at youwanttoremovethisvakantiebaas dot nl
    20-Apr-2008 10:40
    if you do this
    <?php$x = new b();class b extends a {}class a { }?>
    PHP will tell you "class b not found", because you've defined class b before a. However, the error tells you something different.... Got me a little confused :)
      

  7.   

    先使用 , 后定义 -- 当然我觉得不是真的,所以标红了<?php
    $son = new son(); //可以运行!
    $son->say_hello();class father {
    }class son extends father {
        public function __construct() {        
        }    public function say_hello() {
            echo "Son says: hello";
        }
    }
    ?>
      

  8.   

    这样就不行,报错(有点令人困扰):Fatal error: Class 'son' not found in E:\webwork\apps\controller\the9\test.php on line 2================================<?php
    $son = new son();
    $son->say_hello();class son extends father {
        public function __construct() {        
        }    public function say_hello() {
            echo "Son says: hello";
        }
    }class father {
    }

    ?>
      

  9.   

    6 楼的那个不也是extends关键字嘛? 仔细看看,是一样的道理啊
      

  10.   

    PHP对于extends有顺序上的要求,其他的好像没有。
    但是它就是这样的,我们也没办法不是?!
      

  11.   

    PHP对于extends有顺序上的要求,其他的好像没有。 -- 下面的例子可以使用,“先初始化,后定义”,只能说明include函数在解析时出了问题(解析顺序有问题,将class base放到tester之后去了),造成和8楼同样的问题!(我觉得8楼的报错语义欠妥,但报错本身是合乎情理的!)<?php
    $son = new son(); //可以运行!
    $son->say_hello();class father {
    }class son extends father {
      public function __construct() {  
      }  public function say_hello() {
      echo "Son says: hello";
      }
    }
    ?>
      

  12.   

    我认为和习惯没有关系,我也知道很多语言是“先定义后使用”的;但php更为动态,他是先解析(类定义)而后再执行,这样就强制你"先定义后使用"了。语言本身有这样的特色,我当让有想用此种特色的地方--不是在抬杠,如有冒犯,多多原谅!
      

  13.   

    c语言有goto,但是所有大师都对这个语句强烈反感,难道说那些大师不够聪明.这个主题到此为止吧.我也学到了一些东西.
      

  14.   

    与大师无关 任何大学教语言的老师都会要求不用goto 
      

  15.   

    好吧,我就此打住,我从不用goto和指针是因为,web程序不需要特别高的效率和脑力。如果想要挑战自己,就去编写xbox360和ps3的游戏引擎,里面有很多所谓“大师”不齿的goto和指针;不用是因为自己掌握和控制不了。反过来讲:“杀鸡不用牛刀”!
      

  16.   

    用goto又不代表有本事
    这个误区哪来的啊
    lz我觉得你想偏了 
    写个别人不易懂的代码不是牛
      

  17.   

    require_once('test.inc.php');//这里并不会立即编译
    class tester extends base{
    //extends base这里就会立即查找(而不是把所有文档都加载完成了,再进行编译)当前及这以前包含近来的文档里的base类,并编译类所在的文档(准确的说应该不是编译,可以理解为编译)。也就是也编译了var_dump(new tester());但整个文档中的tester类还才只进行到extends base还没有完成这个类的构造,所以系统是认为找不到它!将var_dump(new tester());放在tester类的后面就不会有问题了。}