Reputation: 243
I have been looking into name spaces recently. I am creating an MVC framework and want to try to move towards PHP 5.3+ features.
Lets say I define a namespace, call it \Controller.
I then want to include a file which has the class of Home.
namespace Controller;
include "class.home.php";
The file contents:
class.home.php:
class Home {
public function hello() {
}
}
In this example, will Home be a part of the Controller namespace? or part of the global namespace?
I want to be able to access Home like new \Controller\Home();
Will the code above work like that?
Upvotes: 0
Views: 116
Reputation: 3771
In your case Home will not be part of the Controller namespace, because you need to define the namespace inside of class.home.php like:
namespace Controller;
class Home {
public function hello() {
}
}
And than you can:
namespace Controller;
include "class.home.php";
new \Controller\Home();
Upvotes: 0
Reputation: 522024
The namespace of a class depends on the namespace it was declared in, not in which it was included. If the latter was the case, it'd be impossible to clearly know what namespace something will be in.
If you did not write namespace Foo\Bar\Baz;
at the top of the file in which you declare the class, the class is in the global namespace, always.
Upvotes: 1