I have a files structure like this:
Class (folder):
- User.php
- Rule.php
Scripts (folder):
- user.php (in lowercase)
- otherscript.php
index.php
Rule class (Class/Rule.php):
namespace My\Namespace;
require_once 'Gender.php';
require_once 'User.php';
class Rule
{
...
User class (Class/User.php):
namespace My\Namespace;
require_once 'Gender.php';
class User {
...
In the Rule class I use a ‘require_once’ that includes the User class (require_once ‘User.php’), the problem is that when I include de Rule class in ‘otherscript.php’ it includes me both (script/user.php and class/User.php). How can I avoid this?
You can either
require_oncethe element relative to itself rather the to whatever the current working dir may be:… but it would be even better to just define an autoloader (use the SPL autoloader rather then the old
__autoloadfunction) which takes care of that for you, removing the need for most include/require statements for classes.