note 116203 deleted from language.namespaces.definitionmultiple by salathe
| From: | salathe@php.net | Date: | Fri, 21 Nov 2014 15:01:03 +0000 |
| Subject: | note 116203 deleted from language.namespaces.definitionmultiple by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-200998@lists.php.net to get a copy of this message | ||
Note Submitter: Gilberto Albino
----
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();
?>