I have an older framework which doesn't implement namespaces. If i try to slowly introduce namespaces, by first declaring one at top of a class declaration, then any invokation of that class will now fail, because it isn't invoked through its namespace. Even though the class file is already included, and there isn't (or so i thought) a need for PHP to know the namespace.
It seems PHP doesn't care that it can find a class by the name Foo
, because if Foo
is under a namespace, then it will always mandate that you have to specify the namespace as well.
As a simple test, i put this in one file:
<?php
namespace Test;
class Foo {
function bar(): void {
print "Hello world.";
}
}
And this in another:
<?php
include "Foo.php";
$foo = new Foo;
$foo->bar();
It gives me the following error:
Fatal error: Uncaught Error: Class 'Foo' not found
So my question is, can this behaviour be avoided (with some configuration options maybe) to ease the slow transition from a framework that doesn't use namespaces into one that does, or would I have to replace all invokations of all the classes at once before it becomes usable? Are there any hacky alternatives?
question from:
https://stackoverflow.com/questions/65652355/can-you-avoid-specifying-a-namespace-when-invoking-a-class-which-uses-one 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…