0

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!

2 Answers 2

0

This is a shot from the hip, but:

if( !class_exists( 'SmsService' ) ){ // Class not yet declared - so do it
  interface SmsService {
    public function SendSms($mobile, $customerId, $customerName, $message);
    public function QueryIncomingResponse();
    public function CleanExpiredConfirmations($maxConfirmationDays);
  }
}

Could it be that you are including the PHP file housing the class more than once within the affected sub-site?

6
  • I tried this but it fails misserable since i had to change the interface for customer2 slightly, now the interface for customer1 is used rather than the one for customer2 which ofcourse is wrong
    – Polity
    Nov 14, 2011 at 4:57
  • And no to your second question: again, the error happens on the very first include
    – Polity
    Nov 14, 2011 at 4:58
  • OK, cool - the more mistake we (in this case, I) make the closer we come to a solution. Have you tried, or are you able to, have the CLASS file held centrally, in a location each of your customer sides can access it? If there is only one file with a unique class in it, and you continue to get this error then something extremely screwy is going on. Nov 14, 2011 at 5:02
  • Thanks Lucanos. As i stated, I can't share the file due to different functional implementations (although the same naming). Yet both sites are just subdirectories of 1 site. Now one more observation is this: I can create a new unrelated file like test.php in which i declare a new instance of a derived of SmsService without even including or requiring any file at all. It's like my custom class is now interpreted as a php core class.
    – Polity
    Nov 14, 2011 at 5:07
  • Personally, I would have driven towards keeping the common/core SMS functionality in one class and having localised extensions in each client section, as required, to make any changes. How about renaming the class from "SmsService" to "SmsService2" and see if you still clash? Nov 14, 2011 at 5:27
0

The problem was so obvious (once you know it). Our system administrator added an include path within the php.ini to another project sharing the same classes. Sorry for the trouble!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.