Skip to content

Instantly share code, notes, and snippets.

@ryanuber
Created September 28, 2013 03:35
Show Gist options
  • Save ryanuber/6738085 to your computer and use it in GitHub Desktop.
Save ryanuber/6738085 to your computer and use it in GitHub Desktop.
PHP: "use" statement resolving both class and namespace in one statement. What if you have a namespace called `\fruit\apple`, and also a class called `\fruit\apple` ? If you do `use fruit\apple`, what does it "use" ? The answer is, both. It will now allow you to refer to both `apple` and `apple\...` as you please.
<?php
namespace fruit {
class apple {
public static $x = 'this is the apple class';
}
}
namespace fruit\apple {
class seed {
public static $x = 'this is the seed class';
}
}
<?php
use fruit\apple;
require 'test.php';
print apple::$x . "\n";
print apple\seed::$x . "\n";
@ryanuber
Copy link
Author

The result of this gitst is:

$ php run.php
this is the apple class
this is the seed class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment