1

I have a lot of classes in multiples subfolders that I load using this autoloader :

spl_autoload_register(function ($class) {
    $class = str_replace('\\', DIRECTORY_SEPARATOR, strtolower($class));
    if(file_exists(FILES_PATH.'classes/'.$class.'.class.php')){
        require_once(FILES_PATH.'classes/'.$class.'.class.php');
    }
});

So if i do new Folder\subFolder\Myclass, it works.

The classes in folders are all in a namespace.

All these classes must use the database class, and the problem is here : When the class is in a namespace and search the database class, it can't find it.

(DB class is in global namespace)

So I try to put "use BDD" (Bdd is the db class) and it still doesn't work, because Bdd is using PDO and so i must do "use bdd, pdo;" in EVERY classes of the project...

I find this stupid. Is this normal ? Is there a better way to autoload, without using namespaces ?

7
  • 1
    Consider adopting the PSR coding standards and their autoloader: phpmaster.com/autoloading-and-the-psr-0-standard Commented Aug 12, 2013 at 10:20
  • 1
    Try using global class like this \BDD. It should solve that problem. Commented Aug 12, 2013 at 10:22
  • Yes, each file has its own namespace declarations and therefore its own use imports. No, there's no way around that. No, this is not "stupid". I don't understand why you need to use PDO in every file that uses BDD though. Commented Aug 12, 2013 at 10:28
  • @deceze I also don't understand, But it's not working if I don't put it... Commented Aug 12, 2013 at 10:30
  • Are you referencing PDO anywhere in the file? If not, you don't need to use it. If you're using BDD and it's not in the same namespace as the file, then of course you need to either use fully qualified names (see Rocket's answer) or use it. Commented Aug 12, 2013 at 10:32

3 Answers 3

2

It's pretty darn simple:

If you're in a namespace like so:

namespace Foo;

all names of all classes are resolved relative to that namespace. Any Bar will mean the class Foo\Bar, not the "global" Bar. If you want to refer in any way, shape or form to a class which is not in the same namespace, say Bar\Baz (class Baz from the namespace Bar), you have two choices:

  1. use a fully qualified name for the class, e.g.:

    \Bar\Baz
    

    where the leading \ means the class name shall be resolved from the top namespace, not the current local one, or

  2. if this is getting annoying to do every time, alias it using:

    use Bar\Baz;
    

    which is shorthand for

    use Bar\Baz as Baz;
    

    which means any time you use "Baz" in this namespace you mean the class Bar\Baz, not Foo\Bar\Baz.

Yes, this applies to each file individually. If you want to refer to PDO in some namespace in some file, you either have to write \PDO to make it resolve to the "global" PDO class or you write use PDO at the top of the file to make a convenience alias. That's how namespaces work.

This applies to all use cases of any class name:

new \PDO
\PDO::staticMethod()
\PDO::CONSTANT
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I understand better. But, WTF... Autoloading made me reduce my project for hundreds lines, but namespacing made me copy/paste a line in hundred files ! It's a bit disappointing...
1

You can explicitly say that BDD is in the global namespace by doing this in your code:

$foo = new \BDD();

Then you should not need to use it.

9 Comments

Yes, I've seen that but It's not working. Only use is working... This may be because i'm not doing new Bdd but Bdd::getInstance.
so \Bdd::getInstance does not work? What error are you getting?
Did you try using use PDO or \PDO in BDD class?
I retried and \Bdd::getInsance works, but now the error is on "new PDO", and it's not working even if I do \new PDO.
Not \new PDO, it is new \PDO.
|
0

Moving answer from one of my comments ;)

Using use \Pdo; in BDD class fixed the world :)

1 Comment

I'm sorry, I thought it was good but in fact when I call the Bdd class from a namespaced class, I still have the same error when using a PDO::FETCH function in the namespace... This is really strange. So I think i'm forced to place "use bdd, pdo;" everywhere... and I don't like that. Thanks anyway...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.