当前位置:Gxlcms > PHP教程 > PHP找不到类?

PHP找不到类?

时间:2021-07-01 10:21:17 帮助过:21人阅读

  1. <code> <!--?php
  2. $test = new testString();
  3. var_dump($test--->test1());
  4. class testString extends Father{
  5. public function test1(){
  6. return 1;
  7. }
  8. }
  9. </code>
  1. <code><!--?php
  2. class Father{
  3. public function __construct( ) {
  4. }
  5. }
  6. </code--></code>

为什么会报错?

Fatal error: Class 'testString' not found

如果我去掉extends Father之后就可以正常输出了。

回复内容:

  1. <code> <!--?php
  2. $test = new testString();
  3. var_dump($test--->test1());
  4. class testString extends Father{
  5. public function test1(){
  6. return 1;
  7. }
  8. }
  9. </code>
  1. <code><!--?php
  2. class Father{
  3. public function __construct( ) {
  4. }
  5. }
  6. </code--></code>

为什么会报错?

Fatal error: Class 'testString' not found

如果我去掉extends Father之后就可以正常输出了。

第一,从你贴出来的代码看,你应该是写在两个文件之中,在 testString 类文件中没有 require 或 include Father类文件。
第二,也是你这个问题的关键所在,类声明的顺序,在文件编译过程中,编译到 testString 类声明时,发现没有Father类(即未声明),这时 testString 类声明失败, 这个你可以用class_exists()来验证一下。所以在实例化 testString类的时候报错 class 'testString' not found。

将父类在继承其的子类之前声明就可以了。

因为你的定义在调用的后面呀

肯定错啊。

顺序执行,testString还没定义,你就new了。

请将父类写到子类前面,请在定义类以后在实例化

人气教程排行