Reputation: 6197
Lets suppose I have some classes in the global namespace. I don't want to use the namespace keyword in each class, but I have an autoloading mechanism. Can I dynamically set the namespace for the classes? So
\src\HTML\Form\Form.php
// namespace HTML\Form
class Form
{
}
\src\Setting\Db.php
// namespace Setting
class Db
{
}
Upvotes: 0
Views: 61
Reputation: 36934
I dont want to use the namespace keyword in each class
If you don't want use namespace, then don't use it. If your problem is only to use an autoloader then you can simply map each class with its directory path.
$srcDir = __DIR__. '/src';
$formDir = $srcDir . '/HTML/Form';
$classesMap = array(
'Db' => $srcDir . '/Setting/Db.php',
'Form' => $formDir . '/Form.php',
);
This work would not be necessary if you followed one standard like PSR-0/4 or the (old) PEAR coding standard.
If you're using composer, look classmap.
is there a way to load a class into a namespace?
Something like
function autoload($class) {
namespace DynamicNamespace {
require ('/path/to/classes/'. $class);
};
}
is not allowed.
Upvotes: 2