Create Autoloader using Composer

How to create an autoloader using composer?

  • Create a new file composer.json in the main folder and insert the just curly brackets

  • Run, to generate the autoload structure :
$ composer install
        "autoload" : {
            "psr-4": {
                    "Acme\\" : "src"

Acme is a example of Company/Project name, and src is the directory that contains all the files we want include in our project.

  • Run, to generate the psr-4 autoload file, the command:
$ composer dump-autoload

The content of the file will be the following one:


// autoload_psr4.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'Acme\\' => array($baseDir . '/src'),
  • Require the file autoload.php in your main project file (e.g.: index.php):
require 'vendor/autoload.php';
  • Use namespaces Acme in your project for the file not in subfolder but just in src: So for example if you have the class Test.php in src you just need to use this.
namespace Acme; //in your class called Test

class Test


use Acme\Test; //to use the class Test

You don’t need to use src in your namespace because the autoload has the $basedir on src.

The general rule is >>> what is inside the namespace + Class Name

So for example if you have the folder User in src for the class Person:

You must have the following code structure:

Class Person.php in src/User/:


namespace Acme\User; //Use Acme + User

class Person
    protected $name;

     * @return name
    public function getName(): string
        return $this->name;

    public function setName(string $name)
        $this->name = $name;
        return $this->name;
  • index.php:

    require 'vendor/autoload.php';

    use Acme\User\Person; //Use the namespace + the class name

    $person1 = new Person();
    $person1->setName('John Doe');
    echo $person1->getName();

Install LAMP ubuntu 16.04

Install and configure LAMP

Install apache2:

sudo apt-get install apache2

Set Global ServerName to Suppress Syntax Warnings. Append in /etc/apache2/apache2.conf (bottom of the file):


Launch the command:

sudo apache2ctl configtest

you should receive the message Syntax OK.

Restart Apache to implement the changes:

sudo /etc/init.d/apache2 restart

Adjust the Firewall to Allow Web Traffic

sudo ufw app list

http://your_server_IP_address to see the welcome page from apache.

Install MySQL

sudo apt-get install mysql-server

Set a secure password for the root user and launch the script to remove some dangerous defaults and lock down access to our database system

sudo service mysql restart //restart the service

try the connection to the db with:

mysql -u root -p

Install PHP

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql

Modify the way that Apache serves files when a directory is requested serving php file before instead of html edit the file:


And insert the php index file before:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.xhtml index.htm

restart apache: sudo /etc/init.d/apache2 restart

test php with a phpinfo page.