I'm learning to use php autuoloader
...
As much as I understand, we can use __autoloader
or spl_autoloader_*
to auto load files.
Assume this is my directories structure :
ROOT
|
|
ADMIN
| |
| |
| DIST
| |
| SOME_FOLDER
| SOME_FOLDER
| TPL
| |
| |
| SOME_FOLDER1
| |
| test.php
| SOME_FOLDER2
| |
| example1.php
| example2.php
| example3.php
|
|
CLASSES
|
basics.php
class1.php
class2.php
class3.php
class4.php
|
index.php
I made this class for autoload files in CLASSES
directory :
basics.php :
class MyAutoLoader
{
public function __construct()
{
spl_autoload_register( array($this, 'load') );
}
function load( $file )
{
//spl_autoload( 'load1' );
echo 'Try to call ' . $file . '.php inside ' . __METHOD__ . '<br>';
require( $file . '.php' );
}
}
and in index.php
I will include basics.php
and every thing is fine for files are stored in CLASSES
folder...
require_once("CLASSES/basics.php");
$loaderObject = new MyAutoLoader();
with this code, I can declare class1
...
class3
Now I want to have an autoloder that could be load files in SOME_FOLDER2
which in this case are example1.php
, example2.php
and example3.php
I tried some cases but the files in SOME_FOLDER2
won't be load using autoloader.
My attempts :
I made a function named load2
in MyAutoLoader
class that try to include files from SOME_FOLDER2
function load2( $file )
{
//spl_autoload_register('load2');
echo 'Inside LOADER2 ' . __METHOD__ . '<br>';
require ( 'ADMIN/TPL/' . $file . '.php' );
}
And I changed spl_autoload_register
in MyAutoLoader
constructor :
$allMethods = get_class_methods( 'MyAutoLoader' );
$allMethods = array_splice( $allMethods, 1 );
foreach( $allMethods as $method )
{
spl_autoload_register( array($this, $method) );
}
But none of them didn't work for me...
Would you please tell me what's wrong in my code or what is my misunderstanding about auloader?
Thanks in Advance