note 127183 added to function.spl-autoload-register

From: Date: Sat, 18 Jun 2022 05:25:30 +0000
Subject: note 127183 added to function.spl-autoload-register
Groups: php.notes 
Request: Send a blank email to php-notes+get-218598@lists.php.net to get a copy of this message
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'; } ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.12.131 (proxied: 175.36.56.27) ---- Manual Page -- https://php.net/manual/en/function.spl-autoload-register.php Edit -- https://main.php.net/note/edit/127183 Del: integrated -- https://main.php.net/note/delete/127183/integrated Del: useless -- https://main.php.net/note/delete/127183/useless Del: bad code -- https://main.php.net/note/delete/127183/bad+code Del: spam -- https://main.php.net/note/delete/127183/spam Del: non-english -- https://main.php.net/note/delete/127183/non-english Del: in docs -- https://main.php.net/note/delete/127183/in+docs Del: other reasons-- https://main.php.net/note/delete/127183 Reject -- https://main.php.net/note/reject/127183 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#218598) next »