<?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之后就可以正常输出了。
<?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之后就可以正常输出了。
第一,从你贴出来的代码看,你应该是写在两个文件之中,在 testString 类文件中没有 require 或 include Father类文件。
第二,也是你这个问题的关键所在,类声明的顺序,在文件编译过程中,编译到 testString 类声明时,发现没有Father类(即未声明),这时 testString 类声明失败, 这个你可以用class_exists()来验证一下。所以在实例化 testString类的时候报错 class 'testString' not found。
将父类在继承其的子类之前声明就可以了。
立即学习“PHP免费学习笔记(深入)”;
因为你的定义在调用的后面呀
肯定错啊。
顺序执行,testString还没定义,你就new了。
请将父类写到子类前面,请在定义类以后在实例化
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号