note 116203 added to language.namespaces.definitionmultiple
| From: | GilbertoAlbino | Date: | Fri, 21 Nov 2014 13:34:11 +0000 |
| Subject: | note 116203 added to language.namespaces.definitionmultiple | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200973@lists.php.net to get a copy of this message | ||
When working with multiple namespaces in the same file pay attention for the use of __autoload() :
<?php
// vendor/Namespaces/Test.php
namespace Namespaces\One;
class Test
{
public function __construct()
{
echo __NAMESPACE__, PHP_EOL;
}
}
namespace Namespaces\Two;
class Test
{
public function __construct()
{
echo __NAMESPACE__, PHP_EOL;
}
}
?>
<?php
// index.php
error_reporting(E_ALL);
ini_set('display_errors', true);
function __autoload($class) {
$filename = str_replace('\\', '/', $class) . '.php';
require_once $filename;
}
// Will not work since there are no "One" and "Two" directories!
new \Namespaces\One\Test();
new \Namespaces\Two\Test();
// Works
require_once 'vendor/Namespaces/Test.php';
new \Namespaces\One\Test();
new \Namespaces\Two\Test();
?>
----
Server IP: 72.52.91.14
Probable Submitter: 177.32.116.168
----
Manual Page -- http://php.net/manual/en/language.namespaces.definitionmultiple.php
Edit -- https://master.php.net/note/edit/116203
Del: integrated -- https://master.php.net/note/delete/116203/integrated
Del: useless -- https://master.php.net/note/delete/116203/useless
Del: bad code -- https://master.php.net/note/delete/116203/bad+code
Del: spam -- https://master.php.net/note/delete/116203/spam
Del: non-english -- https://master.php.net/note/delete/116203/non-english
Del: in docs -- https://master.php.net/note/delete/116203/in+docs
Del: other reasons-- https://master.php.net/note/delete/116203
Reject -- https://master.php.net/note/reject/116203
Search -- https://master.php.net/manage/user-notes.php