I’m currently doing this but it doesn’t look right to me:
spl_autoload_register(function ($class_name){
$class_name = str_replace('MyNameSpace\\', '', $class_name . '.php');
require $class_name;
});
Please advise.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The namespace is passed because it has to be. How else would the autoloader function know the difference between
Foo\BarandBaz\Bar? 🙂Your method looks okay if you’re absolutely sure that you won’t ever need to load classes with the same names as those found in
MyNameSpace. The canonical method to autoloading classes involves using the parts of the namespace as file system structure, so that, for example,foo\bar\Bazcan be found at pathfoo/bar/Baz.php.