Sorting out your PHP includes using Inclued
This is the third edition of my weekly PECL package series. Check out my Scream article as well as my Sphinx article to learn about these extensions.
If you have ever inherited spaghetti code or worse, written spaghetti code, the following article is for you. This article is an introduction to the Inclued PECL extension. It helps answer the common question “Where is this include coming from?”, something that I’ve asked myself before when working on some projects.
This extension works by overriding an opcode in Zend, allowing it to log information regarding which files are being included, and from where. This information can be collected using a single function named inclued_get_data() or by setting inclued.dumpdir in php.ini to dump the data of each request.
The final step involves graphing this data to get a view of the include hierarchy. This can be done by converting the JSON encoded output into a dot language file, and then converting it to an image or viewing it with an application such as Graphviz.
To start, we need to install the inclued PECL extension:
1 2 3 4 | mbpro:~ chehodgins$ sudo pecl install inclued-alpha downloading inclued-0.1.0.tar ... [...] install ok: channel://pecl.php.net/inclued-0.1.0 |
And add to php.ini and restart apache:
1 2 3 | extension=inclued.so inclued.enabled=1 inclued.dumpdir=/tmp |
Next, in our web browser we load the page that we wish to analyze. A file named inclued.*.* will be added to /tmp. We will convert to this to a dot file using the gengraph.php script that is included in the PECL package:
1 2 3 | mbpro:tmp chehodgins$ php /usr/local/lib/php/gengraph.php -i inclued.00196.2 Written inclued.out.dot... To generate images: dot -Tpng -o inclued.png inclued.out.dot |
Now we have the choice to either create a png using the dot command or simply opening with Graphviz.
1 | mbpro:tmp chehodgins$ dot -Tpng -o ~/Documents/inclued.png inclued.out.dot |
And a super nice graph of the includes is generated as an image. Here is the graph of the includes in Wordpress (click to view fullscreen):
Notice that there are a lot of includes, but in general there appears to be order. Now let’s check out osCommerce:
This also looks decent. What about magento?
Holy crap, thats a lot of includes!
In conclusion, the inclued PECL extension can be useful in many situations, from trying to understand how and
why a file is being included, to reorganizing your includes by seeing the dependencies. If anything, it can be an
easy way to show off your application/framework’s include structure.



