dqwh1119 2011-11-06 01:22
浏览 35
已采纳

php spl_autoload_register帮助...只是不理解

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

  • 写回答

1条回答 默认 最新

  • dpqg86714 2011-11-06 02:50
    关注

    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:

    1. 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.
    2. Your autoload code must then find the correct file with the correct extension (possibly respecting the php include_path priority that you want).
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100