1

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

4

2 回答 2

2

您需要includerequire带有原始类的文件。否则 PHP 将看不到它。

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

于 2012-07-04T21:14:22.377 回答
1

Still learning the ins and outs of OOP. After a few minutes of research I came across spl_autoload_register in the PHP documentation.

I placed the coreFunctions class in /includes/classes/coreFunctions.class.php and the gallery class in /includes/classes/gallery.class.php

My code then became:

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']);
    }
}

And it works! Would someone care to shed some light on what exactly is happening here that is different from just including coreFunctions?

于 2012-07-04T21:39:04.537 回答