Cannot extend a class located in another file, PHP
- by NightMICU
I am trying to set up a class with commonly used tasks, such as preparing strings for input into a database and creating a PDO object. I would like to include this file in other class files and extend those classes to use the common class' code.
However, when I place the common class in its own file and include it in the class it will be used in, I receive an error that states the second class cannot be found. For example, if the class name is foo and it is extending bar (the common class, located elsewhere), the error says that foo cannot be found. But if I place the code for class bar in the same file as foo, it works.
Here are the classes in question -
Common Class
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;
}
}
Code from individual class
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));
}
}
}
The error when I try it this way is
Fatal error: Class 'gallery' not found in /home/opheliad/public_html/admin/photo-gallery/includes/class.admin_photo-gallery.php on line 10