Installing composer, load custom namespace/class
- Skip this for now
Make sure you have composer.json
file in your root folder, if you did't have those file, refer Composer Install section above.
Open up composer.json file and write down below code :
{
"autoload": {
"classmap": [
"path/to/FirstClass.php",
"path/to/SecondClass.php"
]
}
}
And finally update autoloader using this command : composer update
, and just call/instantiated directly class from defined classmap above.
-
Make sure you have
composer.json
file in your root folder, if you did't have those file, refer Composer Install section above. -
Suppose we have folder directory like below :
ProjectName/ |src/ |----/Db.php -------> this is our class located |----/Api.php -------> this is our class located |vendor/ |----/composer |------/..list of file inside this folder... |----autoload.php -------> autoload file |composer.json -------> need to defined psr4 here |index.php -------> our main file to run later on
-
Declare
namespace
inside both fileDb.php
andApi.php
, give nameEmi
just for this example :
-
Db.php may contains this :
<?php namespace Emi; class Connection { function hey(){ return 'a'; } }
-
Same goes to Api.php
<?php namespace Emi; class Api { function hey(){ return 'b'; } }
-
Then open up
composer.json
file to add autoload properties by usingpsr-4
and write down below code inside :{ .... "autoload" : { "psr-4" : { "Emi\" : "src/" //<-- "Emi\" = this is the name of namespace, and should be forwarded by backslash, and "src/" is the actual folder path that represent of namespace, simply put, composer will find the file having namespace "Emi" inside the folder "src" } } }
-
After finish, run this command to ensure composer update the autoload and cache :
composer update
- Finally, we can create new instance of our Db and Api class directly by using the keyword
use
, before that, we need to loadautoload.php
file from vendor folder. Open upindex.php
from your root directory and write down below code :
<?php
require_once __DIR__ . '/vendor/autoload.php'; // here we load autoload class file from vendor folder
// must be note that all files inside src folder already required by composer
use Emi\Connection; // Here we reference our code. Emi = is namespace name, Connection = is a class name
use Emi\Api; // Here we reference our code. Emi = is namespace name, Api = is a class name
$C = new Connection();
echo $C->hey();
$A = new Api();
echo $A->hey();
-
Done!
-
Oh wait, what if the
src
folder having complex folder structure as exampleDb.php
are located inside their own folder eg :src/Database/Db.php
, like below structure :ProjectName/ |src/ |----/Database |-------/Db.php -------> this is our class located |----/Api.php -------> this is our class located |vendor/ |----/composer |------/..list of file inside this folder... |----autoload.php -------> autoload file |composer.json -------> need to defined psr4 here |index.php -------> our main file to run later on
-
So, in order to make this available again, we need to update a lil bit inside
Db.php
file, write down below code :
<?php
namespace Emi\Database; // we just only need to add parent folder of Db.php located
class Connection {
function hey(){
return 'a';
}
}
So in index.php file now should be like this :
<?php
require_once __DIR__ . '/vendor/autoload.php'; // here we load autoload class file from vendor folder
// must be note that all files inside src folder already required by composer
use Emi\Database\Connection; // ******* change to this, just add respective folder name
use Emi\Api; // Here we reference our code. Emi = is namespace name, Api = is a class name
$C = new Connection();
echo $C->hey();
$A = new Api();
echo $A->hey();
And finally we update again the cache using either this composer update
One thing worth noting is that for large projects the overhead of using the Composer autoloader can become noticeable. This is partly due to the overhead incurred with a lot of file_exists() calls. One way to get around this is to convert the autoloader to use a class map(will create an array on this file autoload_classmap.php
) using the dumpautoload command:
composer dumpautoload -o
This will create a map of all namespaces to their respective files. The downside to this approach is that every time new classes are introduced the class map would need to be re-created, as well as any time the composer install/update commands are ran