简体   繁体   中英

php class autoload - seems to be caching?

I've recently implemented the following code to autoload classes in my php code:

function my_autoloader($class) {
    include 'classes/class_' . $class . '.php';
}
spl_autoload_register('my_autoloader');

The code is contained in file that gets included at the top of all files that need to generate a web page. So then I set about removing the require_once calls from my code. Here's an example:

require_once('classes/class_web_page.php');

As you'll probably gather from the above, the various pages on my site (it's an online community so there's a forum, gallery, etc) all use the web_page class to generate page headings, menus, etc. The various pieces of code create a web_page object, set various parameters to determine what menu options etc are needed, give the page some content, and generate the html.

All seemed well until I made a change to the class_web_page.php file. The change was immediately apparent on most of the site... except for the home page.

I refreshed my browser, switched if off an on again :-) tried calling http://www.terragenesis.co.uk/index.php rather than just http://www.terragenesis.co.uk/ , and even stopped and restarted apache on the server. Despite all this the changes didn't show on the home page. In the end I put the require_once line back into index.php... and presto hey: the changes I'd made in class_web_page.php appeared on the home page.

So it looks to me like the autoloader was loading a cached copy of class_web_page.php and nothing, not even restarting Apache, was going to persuade it to get the new version. Why it should do this for the home page and not the other pages... I have no idea.

Has anybody else experienced this? Have I done something wrong with the autoloader code? How can I fix it... or am I going to have to put all the require_once statements back in place? :-(

My server has PHP version 5.1.6

I found the answer to this... and as usual with "bugs" that elude me for days, it turns out that I did something incredibly stupid:

It turns out that at some point in the past I accidentally uploaded a copy of class_web_page.php into the site home directory rather than the classes subdirectory. So it existed twice.

So, it would appear that despite my autoloader telling php to look in the classes subdirectory, it will look first in the same directory as the main script (index.php in the case of my site's home page). All of the other site page scripts are in subdirectories (forum, gallery, etc) so they were "correctly" using the /classes/class_web_page.php

I've now deleted the copy of class_web_page.php that was living in the home directory... and everything works as it should.

您确定此文件实际已加载(或其缓存版本)吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM