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!
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!
为什么写在test.inc.php文件中?
在test.inc.php中还没有tester类,就执行这句当然会有问题。
base -> tester -> new tester 才是正确的。如果tester不是base的子类,那么执行顺序相当于tester -> new tester,就没有问题了。
24-May-2008 01:35
@info -- 20-AprilThis 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 :)
$son = new son(); //可以运行!
$son->say_hello();class father {
}class son extends father {
public function __construct() {
} public function say_hello() {
echo "Son says: hello";
}
}
?>
$son = new son();
$son->say_hello();class son extends father {
public function __construct() {
} public function say_hello() {
echo "Son says: hello";
}
}class father {
}
?>
但是它就是这样的,我们也没办法不是?!
$son = new son(); //可以运行!
$son->say_hello();class father {
}class son extends father {
public function __construct() {
} public function say_hello() {
echo "Son says: hello";
}
}
?>
这个误区哪来的啊
lz我觉得你想偏了
写个别人不易懂的代码不是牛
class tester extends base{
//extends base这里就会立即查找(而不是把所有文档都加载完成了,再进行编译)当前及这以前包含近来的文档里的base类,并编译类所在的文档(准确的说应该不是编译,可以理解为编译)。也就是也编译了var_dump(new tester());但整个文档中的tester类还才只进行到extends base还没有完成这个类的构造,所以系统是认为找不到它!将var_dump(new tester());放在tester类的后面就不会有问题了。}