Php namespace - repeating Use in each class -
I sometimes have classes in subfolders that I am loading using this auto loader: < Pre> 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> All names in all classes are resolved relative to that namespace. Any Use a fully qualified name for the class, such as: Where major If there is a problem in doing this every time If it is, use it Surname : which is a Macintosh for Which means that you use " Yes, it applies individually to each file if you want to cite This applies to all use cases of any class name: 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')}}}}
Namespace Foo;
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:
\ Bar \ Baz
\ means that the name of the class will be resolved with the top namespace, not the current local, or < Li>
Use Bar / Falcon;
; Bar Baaz is used as a falcon;
hawk " in this namespace, which means you have the class
bar
Foo \ Bar \ Baz .
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.
new \ PDO \ PDO :: staticMethod () \ PDO :: CONSTANT
Comments
Post a Comment