Php namespace - repeating Use in each class -


I sometimes have classes in subfolders that I am loading using this auto loader: < Pre> Spl_autoload_register (function ($ square) {$ class = str_replace ('\\', DIRECTORY_SEPARATOR, stroller ($ square)); if (file_exists (FILES_PATH.'classes /'.$ class'. Class.php ')) {Need_once (FILES_PATH.'classes/'.$ class'. Class.php')}}}}

So if I do new folder / subfolder \ Myclass, So this works.

The squares in folders are all in a namespace.

All of these sections The database class should be used, and here's the problem: when the class is in a namespace and the database finds the square, it can not be found.

(The DB class is in the global namespace) / P>

So I try to use "use BDD" (BDD is DB square) and it still does not work, because BDD is using PDO and therefore I am "bdd, pdo; Should use "; in every class of the project ... < P> I find this stupid. Is this normal? What's the better way to auto-load without using namespaces?

It is very easy:

If you are in the same name: < / P>

  Namespace Foo;   

All names in all classes are resolved relative to that namespace. Any bar will mean the class foo bar , not the "global" bar . If in any way, you do not want to reference the shape or form in the same name space, then bar falcon (class Baz namespace bar ), You have two options:

  1. Use a fully qualified name for the class, such as:

      \ Bar \ Baz   

    Where major \ means that the name of the class will be resolved with the top namespace, not the current local, or < Li>

    If there is a problem in doing this every time If it is, use it Surname :

      Use Bar / Falcon;   

    which is a Macintosh for

     ; Bar Baaz is used as a falcon;   

    Which means that you use " hawk " in this namespace, which means you have the class bar , No Foo \ Bar \ Baz .

    Yes, it applies individually to each file if you want to cite PDO in some namespace in some file You must type "code"> PDO to resolve this "global" PDO code> class or you use the PDO feature on top of the file To make aliases. How this named place works.

    This applies to all use cases of any class name:

      new \ PDO \ PDO :: staticMethod () \ PDO :: CONSTANT    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -