tags:

views:

89

answers:

2
+1  Q: 

__autoload mix up?

I have a server with many customers on, when I develop I include my init.php in which I have an __autoloader() function that includes the file with dir_name(__FILE__)."/classes/".$className for instance.

But yesterday I saw that the server could not find the specific class, I restartat apache and then it worked again.

Every customer has this own init.php... ( and therefore many __autoloads on the same server )

customer1/init.php            : holds __autoload()
customer1/classes/class.php

customer2/init.php            : holds __autoload()
customer2/classes/class.php

I have not done some tests and I hope someone can answer my question before I try to reproduce the problem, but do you think it is possible for php to take the wrong autoload function when you get 2 or more requests at the same time?

Is spl_autoload_register the solution?

Many thanks for some ideas or brainstorming.

+2  A: 

My guess is that you should have a typo in either one of your __autoload() functions or you are including the wrong init.php file.

Also, dir_name() does not exist, you should change that to dirname() instead or you can also use the new DIR constant for the same effect if you're using PHP >= 5.3.

EDIT: In light of your comment, use should use:

require(realpath(dirname(__FILE__)) . '/classes/' . $className);

or

require(realpath(__DIR__) . '/classes/' . $className);
Alix Axel
I never use the absolute path when including the init.php... include (dirname(__FILE__).'/../init.php');
Jhonte
Indeed, I don't know how that skipped my eyes. chdir() or similar should be messing with your code, using the above code should solve your problem.
Alix Axel
Cool, I will try to reproduce my problem tonight, and they try your solution. Thanks for the tips.
Jhonte
A: 

Each PHP request is completely separate, in fact it is impossible for you to have two functions named __autoload() in the same PHP request, so they cannot interfere. Possible problems:

  • You are including the wrong customer's init.php
  • You forgot to include the init.php file, in which case there is no autoloading at all.
too much php
Yeah, but it have worked for about 3 months. Saw it the first time yesterday but I had a similar problem before when I had this structure/test/classes/class.php/test/init.php/classes/class.php/init.phpSo /test/ is a copy of /... When I navigated to /test sometimes I got the same problem with the autoload.... Restarted the server and then it worked for some time until I started surfing on the / and so on...
Jhonte
oh, not so nice formating. :/
Jhonte