Skip to content

Instantly share code, notes, and snippets.

@houssemz
Last active June 10, 2018 16:30
Show Gist options
  • Save houssemz/715183018dd7a971fc94d66b5ace5289 to your computer and use it in GitHub Desktop.
Save houssemz/715183018dd7a971fc94d66b5ace5289 to your computer and use it in GitHub Desktop.
Tricks for namespace
<?php
namespace bar;
class Xyz
{
}
function abc()
{
return __NAMESPACE__;
<?php
namespace foo;
class Xyz
{
}
function abc()
{
return __NAMESPACE__;
}
<?php
namespace bar;
require 'foo/Xyz.php';
require 'bar/Xyz.php';
use foo\Xyz;
use foo\abc;
$x = new Xyz(); // instantiates \foo\Xyz.
$y = new namespace\Xyz(); // the namespace is equivalent to the self operator for classes.
$z = abc(); // invokes \bar\abc regardless of the second use statement.
$u = \foo\abc();
var_dump($x, $y, $z, $u);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment