If you want to be able to run your PHP script from anywhere without changing to the directory first, use chdir(__DIR__) in the first file you run (not any includes):
<?php
spl_autoload_register();
chdir(__DIR__);
For some reason, trying to use namespaced classes from within other namespaced classes fails otherwise.
e.g. Running "php src/main.php" will fail, but "cd src && php main.php" will work.
src/main.php
<?php
spl_autoload_register();
MyNamespace\MyClass::foo();
src/MyNamespace/MyClass.php
<?php
namespace MyNamespace;
class MyClass {
public static function foo(): void {
echo MyOtherClass::BAR; }
}
src/MyNamespace/MyOtherClass.php
<?php
namespace MyNamespace;
class MyOtherClass {
public const BAR = 'baz';
}