PHP速学教程(入门到精通)
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
<code> <?php $test = new testString(); var_dump($test->test1()); class testString extends Father{ public function test1(){ return 1; } } </code>
<code><?php class Father{ public function __construct( ) { } } </code>
为什么会报错?
Fatal error: Class 'testString' not found
如果我去掉extends Father
之后就可以正常输出了。
<code> <?php $test = new testString(); var_dump($test->test1()); class testString extends Father{ public function test1(){ return 1; } } </code>
<code><?php class Father{ public function __construct( ) { } } </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
了。
请将父类写到子类前面,请在定义类以后在实例化
php免费学习视频:立即学习
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!
已抢7337个
抢已抢95475个
抢已抢14939个
抢已抢52728个
抢已抢195931个
抢已抢87512个
抢