一个php include的问题。求解!

eingmarra 2010-06-21 01:29:48
test.inc.php(包含文件):

<?php
class base{
}
var_dump(new tester());
?>




test.php(入口文件):
<?php
require_once('test.inc.php');
class tester extends base{
}
?>

只要将extends base 去掉就可以得到正确的var_dump结果,反之,将出现fatal error:找不到tester类。
只知道现象,求解!
thx!



...全文
183 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dleno 2010-06-22
  • 打赏
  • 举报
回复
require_once('test.inc.php');//这里并不会立即编译
class tester extends base{
//extends base这里就会立即查找(而不是把所有文档都加载完成了,再进行编译)当前及这以前包含近来的文档里的base类,并编译类所在的文档(准确的说应该不是编译,可以理解为编译)。也就是也编译了var_dump(new tester());但整个文档中的tester类还才只进行到extends base还没有完成这个类的构造,所以系统是认为找不到它!将var_dump(new tester());放在tester类的后面就不会有问题了。

}
PIGer920 2010-06-21
  • 打赏
  • 举报
回复
用goto又不代表有本事。。。
这个误区哪来的啊。。。
lz我觉得你想偏了。。。
写个别人不易懂的代码不是牛。。。
honglei8485 2010-06-21
  • 打赏
  • 举报
回复
eingmarra 2010-06-21
  • 打赏
  • 举报
回复
好吧,我就此打住,我从不用goto和指针是因为,web程序不需要特别高的效率和脑力。如果想要挑战自己,就去编写xbox360和ps3的游戏引擎,里面有很多所谓“大师”不齿的goto和指针;不用是因为自己掌握和控制不了。反过来讲:“杀鸡不用牛刀”!
PIGer920 2010-06-21
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 jlzan1314 的回复:]
c语言有goto,但是所有大师都对这个语句强烈反感,难道说那些大师不够聪明.

这个主题到此为止吧.

我也学到了一些东西.
[/Quote]
与大师无关 任何大学教语言的老师都会要求不用goto
jlzan1314 2010-06-21
  • 打赏
  • 举报
回复
c语言有goto,但是所有大师都对这个语句强烈反感,难道说那些大师不够聪明.

这个主题到此为止吧.

我也学到了一些东西.
PIGer920 2010-06-21
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 eingmarra 的回复:]
我认为和习惯没有关系,我也知道很多语言是“先定义后使用”的;但php更为动态,他是先解析(类定义)而后再执行,这样就强制你"先定义后使用"了。语言本身有这样的特色,我当让有想用此种特色的地方--不是在抬杠,如有冒犯,多多原谅!
[/Quote]
这压根就不符合软件工程规范
eingmarra 2010-06-21
  • 打赏
  • 举报
回复
我认为和习惯没有关系,我也知道很多语言是“先定义后使用”的;但php更为动态,他是先解析(类定义)而后再执行,这样就强制你"先定义后使用"了。语言本身有这样的特色,我当让有想用此种特色的地方--不是在抬杠,如有冒犯,多多原谅!
eingmarra 2010-06-21
  • 打赏
  • 举报
回复
PHP对于extends有顺序上的要求,其他的好像没有。 -- 下面的例子可以使用,“先初始化,后定义”,只能说明include函数在解析时出了问题(解析顺序有问题,将class base放到tester之后去了),造成和8楼同样的问题!(我觉得8楼的报错语义欠妥,但报错本身是合乎情理的!)

<?php
$son = new son(); //可以运行!
$son->say_hello();

class father {
}

class son extends father {
public function __construct() {
}

public function say_hello() {
echo "Son says: hello";
}
}
?>
CunningBoy 2010-06-21
  • 打赏
  • 举报
回复
顶楼上,良好的编成习惯可以避免很多错误,省时间。
不能因为语言可以解析,就随便写。
jlzan1314 2010-06-21
  • 打赏
  • 举报
回复
反正我是明白了,楼主你最好抱着先定义后使用的原则的来写程序,因为这是个好习惯.
以后你用到其他语言了,你就知道他的好处了.
CunningBoy 2010-06-21
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 eingmarra 的回复:]

7楼不是活生生的批驳了“先定义后使用”了吗?
[/Quote]
PHP对于extends有顺序上的要求,其他的好像没有。
但是它就是这样的,我们也没办法不是?!
eingmarra 2010-06-21
  • 打赏
  • 举报
回复
7楼不是活生生的批驳了“先定义后使用”了吗?
jlzan1314 2010-06-21
  • 打赏
  • 举报
回复
6 楼的那个不也是extends关键字嘛? 仔细看看,是一样的道理啊
eingmarra 2010-06-21
  • 打赏
  • 举报
回复
这样就不行,报错(有点令人困扰):Fatal error: Class 'son' not found in E:\webwork\apps\controller\the9\test.php on line 2

================================

<?php
$son = new son();
$son->say_hello();

class son extends father {
public function __construct() {
}

public function say_hello() {
echo "Son says: hello";
}
}

class father {
}

?>
eingmarra 2010-06-21
  • 打赏
  • 举报
回复
先使用 , 后定义 -- 当然我觉得不是真的,所以标红了

<?php
$son = new son(); //可以运行!
$son->say_hello();

class father {
}

class son extends father {
public function __construct() {
}

public function say_hello() {
echo "Son says: hello";
}
}
?>
eingmarra 2010-06-21
  • 打赏
  • 举报
回复
http://docs.php.net/manual/zh/language.oop5.basic.php --- PHP文档中搜索到的,似乎说“先定义,后使用”;看看,php中含糊不清的地方还不少,呵呵

====================================

ashraf dot samhouri at hotmail dot com
24-May-2008 01:35
@info -- 20-April

This is because you requested class "b" before defining it, not because you defined class "b" before "a". It doesn't make a difference which class you define first.
info at youwanttoremovethisvakantiebaas dot nl
20-Apr-2008 10:40
if you do this
<?php

$x = new b();

class b extends a {}

class a { }

?>
PHP will tell you "class b not found", because you've defined class b before a. However, the error tells you something different.... Got me a little confused :)
jlzan1314 2010-06-21
  • 打赏
  • 举报
回复
哈哈.不错..学习

好像只有extends 关键字要顺序的.
CunningBoy 2010-06-21
  • 打赏
  • 举报
回复
test.php中tester是base的扩展,所以var_dump(new tester());必须放在tester的定义之后才能正确执行。
base -> tester -> new tester 才是正确的。

如果tester不是base的子类,那么执行顺序相当于tester -> new tester,就没有问题了。
CunningBoy 2010-06-21
  • 打赏
  • 举报
回复
PHP对于class的定义有严格的顺序要求,下面是PHP帮助文件中"extends"部分的注意点:

Note: Classes must be defined before they are used! If you want the class Named_Cart to extend the class Cart, you will have to define the class Cart first. If you want to create another class called Yellow_named_cart based on the class Named_Cart you have to define Named_Cart first. To make it short: the order in which the classes are defined is important.
加载更多回复(2)

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧