I have a files structure like this:
Class (folder):
- User.php
- Rule.php
Scripts (folder):
- user.php (in lowercase)
- otherscript.php
index.php
namespace My\Namespace;
require_once 'Gender.php';
require_once 'User.php';
class Rule
{
...
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_once
the element relative to itself rather the to whatever the current working dir may be:
require_once __DIR__.'/Gender.php';
require_once __DIR__.'/User.php';
... but it would be even better to just define an autoloader (use the SPL autoloader rather then the old __autoload
function) which takes care of that for you, removing the need for most include/require statements for classes.