Last active
June 10, 2018 16:30
-
-
Save houssemz/715183018dd7a971fc94d66b5ace5289 to your computer and use it in GitHub Desktop.
Tricks for namespace
This file contains 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 bar; | |
class Xyz | |
{ | |
} | |
function abc() | |
{ | |
return __NAMESPACE__; |
This file contains 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 foo; | |
class Xyz | |
{ | |
} | |
function abc() | |
{ | |
return __NAMESPACE__; | |
} |
This file contains 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 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