我有一个普通班,我喜欢把它扩展到其他班级。
我的目录设置了这些文件夹中的文件夹和类文件,例如
类别/用户/ users.class.php 类/普通/ general.class.php
我有扩展普通类的用户类,但由于它们位于不同的文件夹中,我猜想找不到常规类。
类用户扩展了一般{
}
有人可以帮我解决这个问题。
我还应该提到我正在使用自动加载功能
答案 0 :(得分:8)
如果您没有自动加载器,请先包含该类。
然后该类已知并且您可以使用它。
require_once(__DIR__.'/../general/general.class.php');
答案 1 :(得分:3)
您需要确保加载所需的类或任何其他类。例如:
在你的引导程序中......:
// Set up the include path so that we can easily access the class files
set_include_path('/absolute/path/to/classes'. PATH_SEPARATOR . get_include_path());
require_once('users/users.class.php');
在users.class.php中:
require_once('general/general.class.php');
class User {
// you class definition
}
至于获取classes文件夹的绝对路径,您需要根据引导位置配置它。例如:
// bootstrap lives in $_SERVER['DOCUMENT_ROOT'] and classes is one level up outside the doc root
// this code is in the bootstrap...
$path = realpath(dirname(__FILE__).'/../classes');
set_include_path($path . PATH_SEPARATOR . get_include_path());