Search the Community
Showing results for tags 'namespaces'.
-
Hi I have just recently tried to incorporate namespaces into my framework but having trouble understanding how I should use them in my project. I have a class called Foo which is part of the namespace \CompanyName\Project. namespace \CompanyName\Project; class Foo { } I have a file called index.php. index.php needs to use that class. index.php handles the autoloading of that class (ive omitted the autoloading code). My index.php looks like below. $foo = new Foo(); This code fails because it can't find class Foo. I can make it work by using the fully qualified name instead. $foo = new \CompanyName\Project\Foo(); I can also make it work by making index.php part of \Company\Project\ namespace. namespace \CompanyName\Project; $foo = new Foo(); Basically my problem is that I have lots of different file includes for individual pages on my site. They all use the old unqualifed class names which are not getting resolved by PHP because it can no longer find them as all of the classes now belong to a namespace. Do I A - Go through every included file and change every class name to the fully qualifed class name? B - Go through every included file and make every file part of the \CompanyName\Project namespace (This feels like the wrong solution)? C - Make PHP treat the old unqualifed class name as a global through use of the 'use' statement. I don't know how to do this. The trouble is there could be 10 - 20 different classes per file which would mean 20 different use declarations. Ideally it would be great if I could import an entire namespace into the global namespace like "use \CompanyName\Project as \;". Basically I want a way to move all classes and functions from a particular namespace into the global namespace so I can use the unqualifed class names throughout the included files. And I want to be able to do it from one file, index.php. I don't think this is possible though as PHP does state I think it will have to be option A , before I begin, am I on the right track?