return __FUNCTION__; } class MyClass { static function WhoAmI() { return __METHOD__; } } echo MYCONST . "<br/>"; // App\Lib1\MYCONST var_dump(MYCONST) . "<br/>"; // string(16) "App\Lib1\MYCONST" echo MyFunction() . "<br/>"; // App\Lib1\MyFunction var_dump(MyFunction()) . "<br/>"; // string(19) "App\Lib1\MyFunction" echo "<br/>" . MyClass::WhoAmI() . "<br/>"; // App\Lib1\MyClass::WhoAmI var_dump(MyClass::WhoAmI()) . "<br/>"; // string(24) "App\Lib1\MyClass::WhoAmI" ?> <?php // myapp.php header('Content-type: text/plain'); require_once 'lib1.php'; // calling lib1.php echo \App\Lib1\MYCONST . "\n"; echo \App\Lib1\MyFunction() . "\n"; echo \App\Lib1\MyClass::WhoAmI() . "\n";
?> <?php namespace App\Lib1; require_once 'lib1.php'; // const, functions and classes here will be called require_once 'lib2.php'; // const, functions and classes here will not be called header('Content-type: text/plain'); echo MYCONST . "\n"; // App\Lib1\MYCONST echo MyFunction() . "\n"; // App\Lib1\MyFunction echo MyClass::WhoAmI() . "\n"; // App\Lib1\MyClass::WhoAmI ?> <?php use App\Lib2; require_once 'lib1.php'; require_once 'lib2.php'; header('Content-type: text/plain'); echo Lib2\MYCONST . "\n"; // App\Lib1\MYCONST ATTENTION ! echo Lib2\MyFunction() . "\n"; // App\Lib2\MyFunction echo Lib2\MyClass::WhoAmI() . "\n"; // App\Lib2\MyClass::WhoAmI ?>
class MyClass { public function WhoAmI() { return __METHOD__; } } $c = __NAMESPACE__ . '\\MyClass'; $m = new $c(); echo $m->WhoAmI() . '<br/>'; // output: App\Lib1\MyClass::WhoAmI var_dump($m->WhoAmI()); // output: string(24) "App\Lib1\MyClass::WhoAmI" ?> <?php // namespace keyword namespace App\Lib1; class MyClass { public function WhoAmI() { return __METHOD__; } } $m = new namespace\MyClass(); echo $m->WhoAmI() . '<br/>'; // output: App\Lib1\MyClass::WhoAmI var_dump($m->WhoAmI()); // output: string(24) "App\Lib1\MyClass::WhoAmI"