! How to Generate PHP Class Inheritance Diagrams in Graphviz % In Linux, I do this by grepping for class definitions and then transforming the grep output to DOT syntax. There have been a series of questions on ^[Stack Overflow] asking how to generate a class diagram for a PHP codebase. The usual aim is to quickly get a sense of how an unfamiliar application is organised. ^ http://stackoverflow.com/questions/2320024/are-there-any-php-code-visualization-tools $figure zend_db.fdp.png Class Structure of of Zend_Db In Linux, I do this by grepping for class definitions and then transforming the grep output to DOT syntax. The script below was for PHP on standard *nix (I used Ubuntu), with ^[graphviz] installed, and using grep -v to exclude some directories that were of no interest because I was looking at a CakePHP codebase. Fdp worked better than sfdp, dot, circo or neato in this situation. This process usually involves some trial and error: the grep chain may need to be tweaked to find the right class definition lines in any given situation. ^ http://www.graphviz.org/ 1. Create a script called ~/shell/generateClassHierarchy.sh TEXT --- #!/bin/bash echo 'digraph code {' > code.dot; grep -r "^class " * | grep -v "^app/vendors" | grep -v "^cake/" | grep -v "Binary file" | sed 's/.*://' | sed 's/class / /' | sed 's/ extends / -> /' | sed 's/ implements .*//' | sed 's/ \?{.*$//' | sort >> code.dot echo '}' >> code.dot; fdp -Tpng -ocode.fdp.png code.dot 2> /dev/null # Ignore syntax error echo "OK"; --- 2. Execute with: TEXT --- cd /var/www/my_app/ # Or whichever codebase. bash ~/shell/generateClassHierarchy.sh # Or wherever you put that. eog code.fdp.png # Or whatever image viewer you prefer. --- + Example Running this on the whole Zend Framework generates a 22 megabyte PNG file, which is not very helpful. Picking something more manageable, here's the DOT file for Zend_Db, which generated the example above. TEXT --- digraph code { Zend_Db_Adapter_Db2_Exception -> Zend_Db_Adapter_Exception Zend_Db_Adapter_Db2 -> Zend_Db_Adapter_Abstract Zend_Db_Adapter_Exception -> Zend_Db_Exception Zend_Db_Adapter_Mysqli_Exception -> Zend_Db_Adapter_Exception Zend_Db_Adapter_Mysqli -> Zend_Db_Adapter_Abstract Zend_Db_Adapter_Oracle_Exception -> Zend_Db_Adapter_Exception Zend_Db_Adapter_Oracle -> Zend_Db_Adapter_Abstract Zend_Db_Adapter_Pdo_Ibm_Db2 Zend_Db_Adapter_Pdo_Ibm_Ids Zend_Db_Adapter_Pdo_Ibm -> Zend_Db_Adapter_Pdo_Abstract Zend_Db_Adapter_Pdo_Mssql -> Zend_Db_Adapter_Pdo_Abstract Zend_Db_Adapter_Pdo_Mysql -> Zend_Db_Adapter_Pdo_Abstract Zend_Db_Adapter_Pdo_Oci -> Zend_Db_Adapter_Pdo_Abstract Zend_Db_Adapter_Pdo_Pgsql -> Zend_Db_Adapter_Pdo_Abstract Zend_Db_Adapter_Pdo_Sqlite -> Zend_Db_Adapter_Pdo_Abstract Zend_Db_Adapter_Sqlsrv_Exception -> Zend_Db_Adapter_Exception Zend_Db_Adapter_Sqlsrv -> Zend_Db_Adapter_Abstract Zend_Db_Exception -> Zend_Exception Zend_Db_Expr Zend_Db_Profiler Zend_Db_Profiler_Exception -> Zend_Db_Exception Zend_Db_Profiler_Firebug -> Zend_Db_Profiler Zend_Db_Profiler_Query Zend_Db_Select Zend_Db_Select_Exception -> Zend_Db_Exception Zend_Db_Statement_Db2_Exception -> Zend_Db_Statement_Exception Zend_Db_Statement_Db2 -> Zend_Db_Statement Zend_Db_Statement_Exception -> Zend_Db_Exception Zend_Db_Statement_Mysqli_Exception -> Zend_Db_Statement_Exception Zend_Db_Statement_Mysqli -> Zend_Db_Statement Zend_Db_Statement_Oracle_Exception -> Zend_Db_Statement_Exception Zend_Db_Statement_Oracle -> Zend_Db_Statement Zend_Db_Statement_Pdo_Ibm -> Zend_Db_Statement_Pdo Zend_Db_Statement_Pdo_Oci -> Zend_Db_Statement_Pdo Zend_Db_Statement_Pdo -> Zend_Db_Statement Zend_Db_Statement_Sqlsrv_Exception -> Zend_Db_Statement_Exception Zend_Db_Statement_Sqlsrv -> Zend_Db_Statement Zend_Db_Table_Definition Zend_Db_Table_Exception -> Zend_Db_Exception Zend_Db_Table_Row_Exception -> Zend_Db_Table_Exception Zend_Db_Table_Rowset_Exception -> Zend_Db_Table_Exception Zend_Db_Table_Rowset -> Zend_Db_Table_Rowset_Abstract Zend_Db_Table_Row -> Zend_Db_Table_Row_Abstract Zend_Db_Table_Select_Exception -> Zend_Db_Select_Exception Zend_Db_Table_Select -> Zend_Db_Select Zend_Db_Table -> Zend_Db_Table_Abstract } ---

Κρήνη εὐκρὰς πρὸς χειμῶνα καὶ θέρος.
A stream of good temperature, summer and winter.

©2012–13 Nigel Chapman, Blue Mountains stream photograph ©2010 Mario Herrmann (used by permission). Time to render page: 1.129 ms.