データベースへの入力用の文字列の準備や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