<?php
$test = new testString();
var_dump($test->test1());
class testString extends Father{
public function test1(){
return 1;
}
}
<?php
class Father{
public function __construct( ) {
}
}
为什么会报错?
Fatal error: Class 'testString' not found
如果我去掉extends Father之后就可以正常输出了。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
第一,从你贴出来的代码看,你应该是写在两个文件之中,在 testString 类文件中没有 require 或 include Father类文件。
第二,也是你这个问题的关键所在,类声明的顺序,在文件编译过程中,编译到 testString 类声明时,发现没有Father类(即未声明),这时 testString 类声明失败, 这个你可以用class_exists()来验证一下。所以在实例化 testString类的时候报错 class 'testString' not found。
将父类在继承其的子类之前声明就可以了。
因为你的定义在调用的后面呀
肯定错啊。
顺序执行,
testString还没定义,你就new了。请将父类写到子类前面,请在定义类以后在实例化