dsj1961061
2012-06-20 10:21
浏览 65
已采纳

spl_autoload_register无法在包含的文件中工作

I'm using spl_autoload_register in order to load classes.

I have an index.php file that includes an init.php file. The spl_autoload_register function is called in the init.php file.

In the index.php file, it works properly: I can create classes and stuff, their names are resolved.

But later on, in the index.php, I include another file, work.php to do some specific task.

Oddly, in the work.php, the class I'm using are not found.

If I call the spl_autoload_register once again in work.php, the class can then be resolved.

The really odd thing about it is that this behavior is not consistent: On my test server, I don't have to duplicate the spl_autoload_register call, but on my production server, it's compulsory.

Am I missing some options in the php.ini?

Edit/Updated: Here is the content on the init.php file, just in case:

<?php
function my_autoload($class){
    include 'class/' . $class . '.class.php';
}

spl_autoload_register('my_autoload');
?>

And my index.php :

<?php
require_once 'include/init.php';

$barcode = new Barcode();
// here is a bunch of test and stuff
include 'work.php';
?>

And my work.php :

<?php
$myObj = new Barcode();
// more useles stuff
?>

The barcode is created flawlessly in the index.php code part, but fails in the work.php part...

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doujuanqi2909 2012-06-20 10:32
    已采纳

    Check your include_path setting if it contains the local directory.

    Generally it is not a good idea to use relative paths. It's easy to create an absolute path that's relative to your init.php file like this:

    function my_autoload($class){
        include __DIR__.'/class/' . $class . '.class.php';
    }
    

    This code assumes that your init.php file is in the same folder as the 'class' folder.

    Also, instead of potentially creating errors with blindly including files that may or may not exist in the given path, check the existence of the file before including it:

    function my_autoload($class){
        $file = __DIR__.'/class/' . $class . '.class.php';
        if(file_exists($file)) {
           include $file;
        }
    }
    

    Please note that __DIR__ is a PHP 5.3 feature which is not be available on hosts with PHP 5.2. You can substitute it with dirname(__FILE__)

    Also, please note that on Linux the file is searched for in a case-sensitive manner, whole on most Mac OS X install, the file is looked for case-insensitively. If you instantiate class MyClass Linux will look for MyClass.class.php while Mac OS X would also load the class if it was in file called myclass.class.php, Myclass.class.php, etc.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • drxzpo70788179614 2012-06-22 09:03

    Well, actually, I'm a dumbass. The problem was not with include path etc, but with an apache configuration : MultiViews.

    My website has a single access point : index.php. And I redirect everything on it using url rewriting. But thanks to multiviews options, url rewriting wasn't working properly if the url was of the same name than the file.

    My directory contains index.php, and work.php.

    My script with rewrite was like that : if you hit www.mywebsite.com/work, you go on index.php with param url=work. index.php initialises everything, then includes work.php

    But thanks to MultiViews options, if I was hitting www.mywebsite.com/work, it would search for file, find a work.php, and call it directly. Meaning : no init, meaning : no spl_autoload_register.

    Thanks for your answers, and sorry.

    评论
    解决 无用
    打赏 举报