Last active
October 26, 2015 10:43
-
-
Save MagePsycho/79136da8a2980b5a5353 to your computer and use it in GitHub Desktop.
PHP5 Namespace / Aliasing explained in a script
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 | |
#@author Raj KB<[email protected]> | |
#ini_set('display_errors',true); | |
#error_reporting(E_ALL | E_STRICT); | |
// You cannot have above because: | |
// Namespace declaration statement has to be the very first statement in the script | |
// How to use namespace? | |
namespace MagePsycho\Easypathhints\Helper; | |
class MyClass {} | |
// Overridden strlen() function | |
function strlen($param) { | |
return '*'; | |
} | |
const MYCONST = 'section/group/field'; | |
$a = new MyClass(); | |
$b = new \MagePsycho\Easypathhints\Helper\MyClass; | |
$c = strlen('hi'); | |
$c2 = \strlen('hi'); | |
$d = namespace\MYCONST; | |
$e = __NAMESPACE__ . '\MYCONST'; | |
// How to use Aliasing / Importing? | |
use MagePsycho\Easypathhints\Helper\MyClass; //as MyClass if no as statement | |
$myClass = new MyClass(); | |
use MagePsycho\Easypathhints\Helper\MyClass as AliasClass; | |
$aliasClass = new AliasClass(); | |
// Results | |
var_dump( | |
$a, | |
$b, | |
$c, | |
$c2, | |
$d, | |
$e, | |
constant($e), | |
$myClass, | |
$aliasClass | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment