This is the same as trying to reference stuff in the global namespace, such as PHPs predefined functions. We shouldn't have to prefix them with "", they should just resolve anyway.
Created
October 17, 2012 22:23
-
-
Save nathggns/3908714 to your computer and use it in GitHub Desktop.
PHP namespaces do not resolve upwards
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace MyProject; | |
| class Animal { | |
| public $sound = false; | |
| public function speak() { | |
| echo $this->sound; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace MyProject\Animal; | |
| class Dog extends Animal { | |
| public $sound = 'whoof'; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Fatal error: Class 'MyProject\Animal\Animal' not found in /Users/Nathaniel/Projects/test/animal/dog.php on line 5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace MyProject; | |
| require 'animal.php'; | |
| require 'animal/dog.php'; | |
| $dog = new Animal\Dog; | |
| $dog->speak(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment