Skip to content

Instantly share code, notes, and snippets.

@MagePsycho
Last active October 26, 2015 10:43
Show Gist options
  • Save MagePsycho/79136da8a2980b5a5353 to your computer and use it in GitHub Desktop.
Save MagePsycho/79136da8a2980b5a5353 to your computer and use it in GitHub Desktop.
PHP5 Namespace / Aliasing explained in a script
<?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