Cannot redeclare class on a copy of a site
Posted
by
Polity
on Stack Overflow
See other posts from Stack Overflow
or by Polity
Published on 2011-11-14T04:07:57Z
Indexed on
2012/06/12
16:40 UTC
Read the original article
Hit count: 202
I've developed a small SMS utility for a customer in PHP. The details are of non-importance.
This project is hosted in: http://project.example.com/customer1
Now a second customer requests almost the same functionality, one cheap way of providing this is to copy the project from the first customer and modify it slightly. So i made a direct copy of the project for customer1 to another folder for customer 2.
This project is hosted in: http://project.example.com/customer2
Now when i try and run the project for customer2 (calling a single page), i get the error message:
Fatal error: Cannot redeclare class SmsService in /var/www/html/project/customer1/application/service.class.php on line 3
Here, service.class.php is a simple interface with 3 methods:
interface SmsService {
public function SendSms($mobile, $customerId, $customerName, $message);
public function QueryIncomingResponse();
public function CleanExpiredConfirmations($maxConfirmationDays);
}
printing the backtrace in service.class.php reveals something interresting:
#0 require_once() called at [/var/www/html/project/customer2/endpoint/queryIncomingResponse.php:2]
Fatal error: Cannot redeclare class SmsService in /var/www/html/project/customer1/application/service.class.php on line 3
Line 2 in queryIncomingResponses is the very first require line there is. Line 3 in service.class.php is the first statement there is in the file (Line 2 is an empty line and line 1 is the php file opening tag).
Naturally, I only work with relative requires (double checked this) so there is no way one include/require from customer2 actually refers to a file for customer1.
It seems to me that in some way SmsService and other classes gets cached by PHP. (I have little control over the server environment). One solution to this would be namespaces. Unfortunatly, we work with PHP 5.1.7 where namespaces are not a part of the language feature just yet. Another way would be to mimic namespaces by prefixing all classes but this approach just feels dirty.
Does anyone have more information on this problem and possibly solutions? Many thanks in advance!
© Stack Overflow or respective owner