I have a folder structure like so:
index.php
app/
controllers/
folder1/
class.php
There is no namespace definition in class.php. Would it be possible to put class.php in a namespace generated from folder structure relative to index? So it would be loaded like:
new \app\controllers\folder1\classInFile();
Or is there no way to dynamically create namespaces?
I.e., you cannot execute any code before the namespace declaration, and afterwards it’s too late. Notwithstanding introspective runtime hacks: no, it’s not possible. Even if it was, it would depend on runtime information, like what folder the code is executed/included from. The namespace could be
var\www\myproject\foo\bar\bazor justfoo\bar\baz. How are you going to determine that? That’s getting messy.Really, just make it explicit, even if that means typing a little more. The namespace is part of the class’s name. You should not generate names dynamically at runtime.