3

I've a very strange issue. In one class "SMSNotifier" I have

require_once (__DIR__ . "/../InvitationNotifier.php");
[...] 
class SMSNotifier extends InvitationNotifier {
[...] 
}

this class is included in another script which is called from the cli. When calling this script I get

PHP Fatal error:  Class 'InvitationNotifier' not found in [...]/include/classi/notifiche/notifiers/SMSNotifier.php on line 12

The strange thing is that if I replace the require_once with a require I get instead

PHP Fatal error:  Cannot redeclare class InvitationNotifier in [...]/include/classi/notifiche/InvitationNotifier.php on line 11

What could be the issue here?

Thank you in advance for any thought. I've ran out of them...

eureka
  • 69
  • 1
  • 7
  • 2
    Is there an `InvitationNotifier` class in `InvitationNotifier.php`? Does it use a namespace? – AbraCadaver Jan 21 '15 at 18:30
  • Yes, there is and no it does not use a namespace. abstract class InvitationNotifier implements Notifier { } – eureka Jan 21 '15 at 18:34
  • What happens when you use the absolute path to the class? – Mr. Concolato Jan 21 '15 at 18:53
  • 1
    Did you place any of your requires inside of a container like a function or a class? I don't know how this scenario would play out: Inside a function, you require_once InvitationNotifier. It is included and the function ends. Now, outside the function, you require_once it again. It was already required, but the class definition was contained inside the function, so it doesn't exists when you tried to use it. Changing to require will define the class in the function (one define) and outside it (a second define - error). – kainaw Jan 21 '15 at 18:53
  • Well actually the require/require_once is outside the class definition and outside any function. I didn't understand your point, if you can elaborate a little – eureka Jan 21 '15 at 23:36

2 Answers2

3

I've continued trying to understand the issue and I've found out that there was a circular dependency. I've "cut it" down and the issue is gone. Hope this can help someone

eureka
  • 69
  • 1
  • 7
1

You should not just be loading up files like it's 1990. Use Composer (PHP) and follow PSR-4 http://www.php-fig.org/psr/psr-4

composer.json

{
  "autoload": {
    "psr-4": {"InvitationNotifier\\": "lib/"}
  }
}

index.php

require_once('autoload.php');
doublejosh
  • 5,548
  • 4
  • 39
  • 45