时间:2021-07-01 10:21:17 帮助过:21人阅读
- <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--></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--></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
了。
请将父类写到子类前面,请在定义类以后在实例化
人气教程排行
-
174次
1
php如何获取跳转前的url
-
174次
2
php格林威治时间转换成当前时间的方法
-
174次
3
为什么php不能做大型系统?
-
174次
4
range函数怎么用
-
174次
5
php中计算页面加载时间几种方法总结_PHP教程
-
174次
6
求帮助,关于paypal支付返回值修改订单状态
-
174次
7
typecho怎么配置文章内容页?
-
174次
8
PhpStorm左侧structure不显示文件的方法列表是这么回事?
-
174次
9
查看PHP的环境变量_PHP
-
174次
10
PHP Primary script unknown 解决方法总结
-
174次
11
php的命名空间与自动加载实现方法
-
174次
12
解决laravel 出现ajax请求419(unknown status)的问题
-
173次
13
php 如何删除mysql记录
-
173次
14
PHP如何替换数组中的指定元素
-
173次
15
怎么去除字符串中非汉字、非字母、非数字的字符
-
173次
16
mysql如何一次执行多条SQL语句
-
173次
17
修改header里面的Connection为close解决方法
-
173次
18
PHP基于session.upload_progress 实现文件上传进度显示功能详解
-
173次
19
php5.6.x到php7.0.x特性小结
-
172次
20
php为什么会出现504错误