Git Product home page Git Product logo

step-composer's Introduction

Installing composer, load custom namespace/class

alt text Composer Install

  • Skip this for now

alt text Add our custom class using standard autoload(same like require declarative) - not recommended

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.

alt text Add our custom class/namespace using PSR4 standard - recommended

  1. Make sure you have composer.json file in your root folder, if you did't have those file, refer Composer Install section above.

  2. 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

  3. Declare namespace inside both file Db.php and Api.php, give name Emi 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';
        }
    }
  1. Then open up composer.json file to add autoload properties by using psr-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" } } }

  2. After finish, run this command to ensure composer update the autoload and cache :

  • composer update
  1. Finally, we can create new instance of our Db and Api class directly by using the keyword use, before that, we need to load autoload.php file from vendor folder. Open up index.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();
  1. Done!

  2. Oh wait, what if the src folder having complex folder structure as example Db.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

  3. 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

alt text Performance. Must read!!

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

step-composer's People

Contributors

metallurgical avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.