Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Autoloading subfolder

Im doing this

spl_autoload_register();

I have a folder called libs where all my files go like : user.php / orm.php etc and inside each class I have it like this

namespace libs;
class User { }

Calling the class like this

$app = new libs\User;

That seems to work but what should I do if for example I want to autoload a class that its not just inside libs folder, its for example inside :

libs / example / example.php

How should I autoload that class too?

like image 545
Raggaer Avatar asked Jul 10 '26 18:07

Raggaer


1 Answers

Provided the class definition and namespace looks like this...

namespace libs\example;

class Example { ... }

The autoloader should be able to find it.

Note that the default spl_autoload lowercases the namespaces and class names so your file names and paths should all be lowercase (which you already appear to be doing but just thought I'd mention it).

Also note, for consistency, you should also set the appropriate autoload extensions, eg

spl_autoload_extensions('.php');
spl_autoload_register();
like image 65
Phil Avatar answered Jul 13 '26 11:07

Phil