Skip to content

Instantly share code, notes, and snippets.

@nathggns
Created October 17, 2012 22:23
Show Gist options
  • Select an option

  • Save nathggns/3908714 to your computer and use it in GitHub Desktop.

Select an option

Save nathggns/3908714 to your computer and use it in GitHub Desktop.
PHP namespaces do not resolve upwards

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.

<?php
namespace MyProject;
class Animal {
public $sound = false;
public function speak() {
echo $this->sound;
}
}
<?php
namespace MyProject\Animal;
class Dog extends Animal {
public $sound = 'whoof';
}
Fatal error: Class 'MyProject\Animal\Animal' not found in /Users/Nathaniel/Projects/test/animal/dog.php on line 5
<?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