Composer part-1: Autoload manager for PHP code

PHP is a very easy and powerfull language for building web-application. I like it the way php code can be embed in html. But if we put all logic in along with HTML ,it is hard to reuse the logic also it is hard to maintain the code and there will be mouse scrolls up and down in a large file. So what is the solution to overcome this problem. I have separated the logic in different files where i created different functions and classes to reuse some code. So for that i have to include these files if i need to use these functions and classes.

It’s okay to add these files using include or require statements available in PHP if there are small numbers of files in one fix folder in your project. But if there to many different folders for the files which you are including in your main page it would be difficult to maintain when your project expanding.

But thanks to composer, it has reduces to much of my effort to include files and classes which i write for my application. Composer is a utility application for managing dependencies for PHP application. It is also available for Windows, Linux and Mac. Just goto the site and install it by reading it’s guide.

If you are done with setting up composer in your system, let’s take a short example to understand how it’s autoload files and reduce our effort for writing too much include or require statements in PHP.

Create composer.json file at root of your project directory. I use below type of directory structure for my PHP projects.

  • Project root / app: all my projects php files will sit here.
  • Project root / app/partials: all partials like header.php, footer.php, etc…
  • Project root / app/img: Images used in my project will be sit here.
  • Project root / app/js: JavaScript used in my project will be sit here.
  • Project root / app/css: css used in my project will be sit here.
  • Project root / lib/classes: PHP classes used in my project will be sit here.
  • Project root / lib/files: common PHP files for reusable functions used in my project will be sit here.
  • Project root / composer.json: This is composer.json file, required for composer to understand the depeandies and autoload features.

Now let see how very basic composer.json file will look like for this type of setup.

Right now just create this file we will understand it later.
Now lets create one class in Project root / lib/classes /module1 directory. Let say product.php.
We can create as many folder as needed in classes and add in composer.json to autoload classes.

please note that in above code we have used namespace which is very powerful feature and available in java as package to avoid name collision.

We are also creating some library functions to use in our project so let create a file lib_functions.php in Project root / lib/files

Now we want to use Product class and these lib_functions available in our application code with lesser amount of require or include lines. So to achieve this we will change our composer.json file as below.

Please note that for classes we just need to pass directories list and files we have to add filenames in composer.json.
Now open command propmpt and goto the root of your project and type below command

  • composer install
  • composer dump-autoload

after executing above commands you will see vendor directory at root of your project. Also you can find autoload.php file and composer directory generated by composer inside vendor. You do not need to change anything here. Now you have to include this autoload.php files inside your php file to use classes and files define in composer.json.

Now lets use Product class and some function from lib_functions.php file create product_controller.php(you can give any name) inside Project root / app and paste below code in it.

Now lets understand above code. Notice the first line which is generated by composer as we see earlier. So we donot need to include all the classes and files where we have write your library functions.

As you see while adding Product class we have added namespace as a firstline in product.php file. We if we want to use Product class we have write use namespace_name statement. That’s why we have written
use classes\module1\Product;
Now every thing is auto-loaded so rest of the code is well understood by PHP programmer.
now run this product_controller.php and you will get result.

I hope you understand the facility provided by composer for PHP developer. In this post we have just seen autoloading feature but composer is more than this. So in next post we will see composer as a dependency manger for managing and installing different packages in our PHP web-application. So stay tuned here.



2 thoughts on “Composer part-1: Autoload manager for PHP code

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s