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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)