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:

<?php

// 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.
//Test.php
namespace Acme; //in your class called Test

class Test
{

}

//index.php
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/:

<?php

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:
<?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();

Fontawesome icons inside a palaceholder

This is one way to use fontawesome icons inside a palaceholder:

in the CSS you have to use this code for the class fontAwesome:

.fontAwesome {
font-family: 'Helvetica', FontAwesome, sans-serif;
}

After that in the HTML you have to add the **fontawesome class** first and then the **fontawesome icon code** inside the placeholder:

<input class="fontAwesome" name="emailAddress" type="text" placeholder="[insert-fa-code-here;] insert email address ..." />

and it’s done!!!

You can simply apply the class every time inside your input with the relative code in the placeholder.

And even more, you can use this method by entering the code into a generic element such as a link or a simple text using the same class “fontAwesome” used for the placeholder:

<a class="fontAwesome" href="#">[insert-fa-code-here;] Great job!!!</a>

You can see the result or read the post from my codePen post.

See the Pen FontAwesome and placeholders by Massimiliano Ranauro (@huckbit) on CodePen.