Reputation: 9230
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
Upvotes: 1
Views: 2580
Reputation: 9230
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?
Upvotes: 1
Reputation: 174957
You'll need to include
or require
the file with the original class. Otherwise PHP won't see it.
Make sure the include is successful, enable error reporting to see errors, or use require
to trigger a fatal error on fail.
Upvotes: 2