Skip to content

Tutorial Autoloading

Everett Griffiths edited this page Feb 22, 2015 · 5 revisions

Tutorial: Autoloading and Composer

Because the existing Composer tutorials leave something to be desired, let's strip this down to the bare bones and walk through a use-case.

  1. Create a dedicated folder for your sample project.

  2. Inside that folder, create a composer.json file and paste in the example from above.

  3. Create the sub-directories where your PHP classes will live. In the above example, we're using a directory named "src/".

  4. Run composer install from within that directory. This is a terminal command, so you first need to navigate to the correct directory using cd. If you have had trouble downloading and installing composer, the simplest alternative is to download the composer.phar file to your local directory. (See Installing Composer). When running the command from the phar file, modify it to this: php composer.phar update

After composer has run, it should create a "vendor" directory. You should never need to edit the contents of this directory: leave that to composer.

  1. Inside your src/ directory, create a file named "MyModel.php":
class MyModel {
    public function test() {
       return 'Hello?';
    }
}
  1. Inside your src/ directory, create a file named "index.php":
<?php
// Adjust the path to your vendor/autoload.php as needed
require_once dirname(dirname(__FILE__)).'/vendor/autoload.php';
$MyModel = new MyModel();
print $MyModel->test();
?>
  1. Execute your index.php file via the command line (e.g. php src/index.php) or hit in in a browser (presuming your package directory is inside a web root).

The expected result here is that the MyModel.php class should be autoloaded automatically without you having to explicitly require it and the script should output "Hello?" without any special include or require statements.

This is a process you may need to repeat several times while working on your project. Depending on your setup, you may need to refresh the autoload settings by running the following command:

composer dump-autoload

Or, if you are using the phar:

php composer.phar dump-autoload

Autoloading and MODX

One trick here is that MODX's file naming convention is not consistent with Composer's: Composer's conventions are that there should be one class per file and the filename should match the classname exactly, case and all (the file will have the ".php" extension). MODX by comparison names its files in all lowercase and with a ".class.php" extension.

However, Composer does support the MODX convention, so if you want to autoload your model classes, you do it by updating your "autoloading" section to match the directory structure your package is using.

The old-school traditional MODx directory structure would look like this:

    "autoload": {
        "classmap":["core/components/mypkg/model/mypkg/"]
    }

However, Repoman allows the use of a simplified directory structure would put our core in a subfolder off of our repository root, inside the "model/" directory. To customize your package structure, you will want to set a couple Repoman-specific attributes (i.e. attributes that are not used by composer):

    {
        "autoload": {
            "classmap":["model/"]
        }
    }

WARNING: Remember to run composer dump-autoload after editing your composer.json's autoload configuration.

See the dedicated page on Autoloading.