php spl_autoload_register help… just not understanding
-
22-02-2021 - |
Domanda
my class naming convention: class.ClassName.php
my class file naming convention: class.classname.php (hence the strtolower).
the class files are located in the include path: /home/content/XX/XXXXXX/html/projects/include/
//autoload.php
<?php
class Autoload {
public static function autoloadClasses($className) {
$className = strtolower($className);
$file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'include/class.' . $className. '.php';
require_once($file);
}
}
$register = spl_autoload_register('Autoload::autoloadClasses');
?>
//check4.php
<?php
$company = $s->company;
$projectName = 'development';
$items = array('type', 'scope', 'table', 'conditions');
$things = array('select', '*', 'todos', array('company'=>$company, PROJECT_NAME=>$projectName));
$combinedArray = array_combine($items, $things);
$q = new Query($combinedArray);
?>
verified classes exist and are included http://technicheian.com/images/includedClasses.png
on every page that makes use of a class (e.g. this one calling Query; located class.query.php:
05-Nov-2011 20:18:30]PHP Fatal error: Class 'Query' not found in /home/content/XX/XXXXXX/html/projects/check4.php
on line 9
at the end of class.session.php is $s = new Session
(noted here to say that the $company variable should not be empty).
I've read pretty much every article, how-to, etc. I can find. What am I missing?
running php 5.2
Soluzione
delete the file_exists
check and look at your error logs. You will see which file you were trying to require. I think it may be confusion on what __FILE__
does (because it is working on the autoload.php file).
Edit: It looks like this
dirname(__FILE__) . DIRECTORY_SEPARATOR . 'include/class.'
should be changed to
/home/content/XX/XXXXXX/html/projects/include/class.
There are a number of things for you to check to ensure that autoloading works:
- For your custom autoloading to be active it must be registered. So the
spl_autoload_register('Autoload::autoloadClasses')
must be executed. A good way of doing this is to edit php.ini and set the auto_prepend file to call your autoload file. - Your autoload code must then find the correct file with the correct extension (possibly respecting the php include_path priority that you want).