我在同一个文件夹中有几个文件:
test.php的:
function __autoload($class_name) {
include $class_name . '.php';
}
$b = new B();
echo 'done' . PHP_EOL;
a.php只会:
echo 'include A' . PHP_EOL;
abstract class A {
function __construct() {
}
}
require_once ('A.php');
require_once ('B.php');
require_once ('C.php');
echo 'A exists - ' . (class_exists('A') ? 'yes' : 'no') . PHP_EOL;
echo 'B exists - ' . (class_exists('B') ? 'yes' : 'no') . PHP_EOL;
echo 'C exists - ' . (class_exists('C') ? 'yes' : 'no') . PHP_EOL;
B.php:
echo 'include B' . PHP_EOL;
final class B extends A {
}
C.php
echo 'include C' . PHP_EOL;
final class C extends A {
}
结果如果我运行test.php:
include B
include A
include C
A exists - yes
B exists - no
C exists - yes
done
为什么B类还不存在?
是因为它在声明代码中吗?
只需添加包含'A.php'即可轻松修复; test.php中的第一行。