我正在尝试设置一个具有常用任务的类,例如准备输入数据库的字符串和创建 PDO 对象。我想将此文件包含在其他类文件中,并扩展这些类以使用公共类的代码。
但是,当我将公共类放在它自己的文件中并将其包含在将要使用的类中时,我收到一个错误,指出找不到第二个类。例如,如果类名是foo
并且它正在扩展bar
(公共类,位于其他地方),则错误表示foo
找不到。但是,如果我将类的代码bar
放在与 相同的文件中foo
,它就可以工作。
以下是有问题的课程 - 普通课程
abstract class coreFunctions {
protected $contentDB;
public function __construct() {
$this->contentDB = new PDO('mysql:host=localhost;dbname=db', 'username', 'password');
}
public function cleanStr($string) {
$cleansed = trim($string);
$cleansed = stripslashes($cleansed);
$cleansed = strip_tags($cleansed);
return $cleansed;
}
}
个别类的代码
include $_SERVER['DOCUMENT_ROOT'] . '/includes/class.core-functions.php';
$mode = $_POST['mode'];
if (isset($mode)) {
$gallery = new gallery;
switch ($mode) {
case 'addAlbum':
$gallery->addAlbum($_POST['hash'], $_POST['title'],
$_POST['description']);
}
}
class gallery extends coreFunctions {
private function directoryPath($string) {
$path = trim($string);
$path = strtolower($path);
$path = preg_replace('/[^ \pL \pN]/', '', $path);
$path = preg_replace('[\s+]', '', $path);
$path = substr($path, 0, 18);
return $path;
}
public function addAlbum($hash, $title, $description) {
$title = $this->cleanStr($title);
$description = $this->cleanStr($description);
$path = $this->directoryPath($title);
if ($title && $description && $hash) {
$addAlbum = $this->contentDB->prepare("INSERT INTO gallery_albums
(albumHash, albumTitle, albumDescription,
albumPath)
VALUES
(:hash, :title, :description, :path)");
$addAlbum->execute(array('hash' => $hash, 'title' => $title, 'description' => $description,
'path' => $path));
}
}
}
我以这种方式尝试时的错误是
Fatal error: Class 'gallery' not found in /home/opheliad/public_html/admin/photo-gallery/includes/class.admin_photo-gallery.php on line 10