1

データベースへの入力用の文字列の準備やPDOオブジェクトの作成など、一般的に使用されるタスクを使用してクラスを設定しようとしています。このファイルを他のクラスファイルに含め、それらのクラスを拡張して、共通のクラスのコードを使用したいと思います。

ただし、共通クラスを独自のファイルに配置し、それを使用するクラスに含めると、2番目のクラスが見つからないというエラーが表示されます。たとえば、クラス名がで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

includeまたはrequire、元のクラスのファイルを作成する必要があります。そうしないと、PHPはそれを認識しません。

インクルードが成功したことを確認するか、エラーレポートを有効にしてエラーを確認するか、require失敗時に致命的なエラーをトリガーするために使用します。

于 2012-07-04T21:14:22.377 に答える
1

まだOOPの詳細を学んでいます。spl_autoload_register数分の調査の後、私はPHPのドキュメントに出くわしました。

クラスをに配置し、クラスcoreFunctionsをに配置しました/includes/classes/coreFunctions.class.phpgallery/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を含めるだけとは異なり、ここで何が起こっているのかを正確に明らかにしたいと思いませんか?

于 2012-07-04T21:39:04.537 に答える