Voting

: max(nine, six)?
(Example: nine)

The Note You're Voting On

Daniel Klein
3 years ago
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
();
//chdir(__DIR__); // Uncomment this to make it work from any directory
MyNamespace\MyClass::foo();

src/MyNamespace/MyClass.php
<?php
namespace MyNamespace;

class
MyClass {
public static function
foo(): void {
echo
MyOtherClass::BAR; // This will fail even if MyOtherClass.php is in the right place
}
}

src/MyNamespace/MyOtherClass.php
<?php
namespace MyNamespace;

class
MyOtherClass {
public const
BAR = 'baz';
}

<< Back to user notes page

To Top