无法扩展位于另一个文件PHP中的类
我正在尝试使用常用任务设置一个类,例如准备字符串以输入数据库和创建PDO对象。我希望在其他类文件中包含此文件,并扩展这些类以使用公共类的代码 但是,当我将公共类放在它自己的文件中并将其包含在它将要使用的类中时,我收到一个错误,表明找不到第二个类。例如,如果类名为无法扩展位于另一个文件PHP中的类,php,oop,Php,Oop,我正在尝试使用常用任务设置一个类,例如准备字符串以输入数据库和创建PDO对象。我希望在其他类文件中包含此文件,并扩展这些类以使用公共类的代码 但是,当我将公共类放在它自己的文件中并将其包含在它将要使用的类中时,我收到一个错误,表明找不到第二个类。例如,如果类名为foo,并且扩展了bar(位于别处的公共类),则错误表示找不到foo。但是如果我将classbar的代码与foo放在同一个文件中,它就可以工作了 以下是相关课程: 普通类 abstract class coreFunctions {
foo
,并且扩展了bar
(位于别处的公共类),则错误表示找不到foo
。但是如果我将classbar
的代码与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));
}
}
}
来自单个类的代码
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));
}
}
}
我这样做的错误是
致命错误:在第10行的/home/opheliad/public\u html/admin/photo gallery/includes/Class.admin\u photo-gallery.php中找不到类“gallery”
您需要包含原始类的文件。否则PHP不会看到它
确保包含成功,启用错误报告以查看错误,或使用require
在失败时触发致命错误。仍在学习OOP的细节。经过几分钟的研究,我在PHP文档中发现了spl\u autoload\u register
我将coreffunctions
类放在/includes/classes/coreffunctions.class.php
中,将gallery
类放在/includes/classes/gallery.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']);
}
}
而且它有效!是否有人愿意解释一下这里到底发生了什么不同于只包含核心函数?不是答案,但这种设计确实需要一些改进。这只是一个更大类的一部分,尚待构建。试图避免大量冗余。这一点我还在学习:)他应该尝试回显$\u服务器['DOCUMENT\u ROOT']
。我怀疑它可能包含一个尾随的斜杠,这会打断路径。@NightMICU:然后呢?没有错误,什么都没有?脚本正常执行(直到您通常遇到的错误发生)?@JezenThomas:是的,但这意味着require将失败。路径是正确的,这与我对所有内容的使用相同。include/require/include_once等完全没有错误。只是说无法找到类库,请尝试在旧代码中将类定义块移动到脚本的顶部。我认为重点是,在定义一个新对象的类之前,你要尝试实例化它。啊,好的想法。实际上,我非常喜欢这种设置,使一切都更有条理,并将其保存为IDE中的代码片段。如果我记得的话,我可能会试一试,然后再报告