无法扩展位于另一个文件PHP中的类

时间:2012-07-04 21:12:41

标签: php oop

我正在尝试使用常用任务设置一个类,例如准备输入数据库的字符串以及创建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

2 个答案:

答案 0 :(得分:2)

您需要includerequire原始班级的文件。否则PHP不会看到它。

确保包含成功,启用错误报告以查看错误,或使用require在失败时触发致命错误。

答案 1 :(得分:1)

还在学习OOP的来龙去脉。经过几分钟的研究后,我在PHP文档中遇到了spl_autoload_register

我将coreFunctions课程安排在/includes/classes/coreFunctions.class.php,将gallery课程安排在/includes/classes/gallery.class.php

我的代码变成了:

function cvfdAutoloader($class) {
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/classes/' . $class . '.class.php';
}

spl_autoload_register('cvfdAutoloader');
$mode = $_POST['mode'];

if (isset($mode)) {
    $gallery = new gallery;
    switch ($mode) {
        case 'addAlbum':
            $gallery->addAlbum($_POST['hash'], $_POST['title'],
                               $_POST['description']);
    }
}

它有效!有人会关注这里究竟发生了什么,这与仅仅包括coreFunctions有什么不同?