namespaces in PHP and autoloader

References

About namespaces:

About autoloading:

About SPL:

When the autoloader is located in the folder holding your classes, then use the relative namespace for the class. For instance, if all your classes are in a Vendor folder and you want to load the User class with the path Vendor/Model/user.php, then the classname to load is ‘Model\User’. In the example below the myautoloader.php file is held in the Vendor directory.

namespace Vendor;

class MyAutoloader
{
public static function autoload(){

    spl_autoload('Model\User');

}
}
spl_autoload_register('Classes\MyAutoloader::autoload');


$conn= new Connect\Connect;

Now we would need to recursively fetch the classes in the directory to load them automatically.

File System Iterator

References:

$pathTop = __DIR__; //pathTop because we will recursively loop through directory starting from the current file directory at the top most category. Check get_url snippet to change that or use substr()

$directory = new RecursiveDirectoryIterator($pathTop, RecursiveDirectoryIterator::SKIP_DOTS); // this return an iterator sarting in the current file directory
var_dump($directory);
echo $directory;

$fileIterator = new RecursiveIteratorIterator($directory,RecursiveIteratorIterator::LEAVES_ONLY);

var_dump($fileIterator);
	
foreach($fileIterator as $file){
	echo $file.'<br>';
	$path_array=explode('\\',$file);
	echo $path_array[count($path_array)-2];
	echo '<br>';
	echo $path_array[count($path_array)-1];
}