11.08.2017 Views

codebright

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The Primers 22<br />

1 "suggest": {<br />

2 "xmen/gambit": "1.0.0"<br />

3 }<br />

Your package might have a number of extra packages that enhance its functionality, but aren’t strictly<br />

required. Why not add them to the suggest section? Composer will mention any packages in this<br />

section as suggestions to install when running the Composer install command.<br />

Well that’s all I have on dependencies. Let’s take a look at the next piece of Composer magic.<br />

Autoloading!<br />

Auto Loading<br />

By now we have the knowledge to enable Composer to retrieve our package dependencies for us,<br />

but how do we go about using them? We could require() the source files ourselves within PHP,<br />

but that requires knowing exactly where they live.<br />

Ain’t nobody got time for dat. Composer will handle this for us. If we tell Composer where our<br />

classes are located, and what method can be used to load them then it will generate its own autoload,<br />

which can be used by your application to load class definitions.<br />

Actions speak louder than words, so let’s dive right in with an example.<br />

1 "autoload": {<br />

2<br />

3 }<br />

This is the section in which all of our autoloading configurations will be contained. Simple, right?<br />

Great! No sock puppetry for you.<br />

Let’s have a look at the simplest of loading mechanisms, the files method.<br />

1 "autoload": {<br />

2 "files": [<br />

3 "path/to/my/firstfile.php",<br />

4 "path/to/my/secondfile.php"<br />

5 ]<br />

6 }<br />

The files loading mechanism provides an array of files which will be loaded when the Composer<br />

autoloader component is loaded within your application. The file paths are considered relative to<br />

your project’s root folder. This loading method is effective, but not very convenient. You won’t<br />

want to add every single file manually for a large project. Let’s take a look at some better methods<br />

of loading larger amounts of files.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!