PHP Components: Shipping Reusable PHP Code
In my Beyond Frameworks talk, I explained how a component-based architecture can help answer some of the important (i.e. expensive!) questions you might face when creating long-lived apps that rely on a PHP framework. In this series of blog posts, I'm going to look at how to go about creating and working with components.
I'm now going under the bonnet of our components, and looking at the different file roles that the PEAR installer expects to find when we distribute our component as a PEAR-compatible package. To get started, I'm going to start with the obvious file role: the PHP code that a component is all about.
What Is PHP Code?
PHP code (the aphp' file role supported by the PEAR installer) is your component's PHP code. This is the code that you want everyone to download and install and then include into their own PHP apps.
Where Does PHP Code Go Inside The Component's Structure?
If we take a look at the CommandLineLib component, you'll find all of the PHP code for this component under the src/php/ folder:
src/php/ is meant to be the top of a tree structure that any PSR0-compliant autoloader could load from:
- The first level of folders under src/php/ is (by convention) the name of the vendor of the code. For CommandLineLib, the vendor is Gradwell.
- The second level of folder onwards can be whatever you need them to be. By convention, for our components at work, we are using the second level of folders to be the name of the component, in this case CommandLineLib. This works really well as you start to build up a larger number of components in your PEAR channel.
- And eventually, you get down to the actual class definitions themselves.
The PHP class Gradwell\CommandLineLib\CommandLineParser therefore ends up in the file src/php/Gradwell/CommandLineLib/CommandLineParser.php.
Where Does PHP Code Get Installed?
When you use the PEAR installer to install your component:$ pear install Gradwell/CommandLineLib
all of the files in your component's src/php/ folder gets installed into /usr/share/php on your computer:
The PHP class Gradwell\CommandLineLib\CommandLineParser therefore ends up installed onto your computer as /usr/share/php/Gradwell/CommandLineLib/CommandLineParser.php.
The reason why the PEAR installer installs your PHP code under /usr/share/php/ is because this folder is normally part of PHP's include_path. That allows developers to include your code simply by doing:include "Gradwell/CommandLineLib/CommandLineParser.php";
or, better still, by using a PSR0-compliant autoloader, and then simply attempting to create a new instance of the class:// load Gradwell's PSR0-compliant autoloader
// create a new command line parser
$parser = new \Gradwell\CommandLineLib\CommandLineParser();
Some Linux distros (and Mac OS X) will use /usr/lib/php/ instead of /usr/share/php/. You can see where the PEAR installer will put these files on your computer by running:$ sudo pear config-show | grep php_dir PEAR directory php_dir /usr/lib/php