Understanding large php code, what techniques to use? Understanding large php code, what techniques to use? php php

Understanding large php code, what techniques to use?


First, in this kind of situation, I try to get an overview of the application : some kind of global idea of :

  • What the application (not the code !) does
  • How the code is globally organized : where are the models, the templates, the controllers, ...
  • How each type of component is structured -- once you know how a Model class works, others will typically work the same way.


Once you have that global idea, a possibility to start understanding how the code works, if you have some time before you, is to use a PHP Debugger.
About that, Xdebug + Eclipse PDT is a possibility -- but pretty much all modern IDEs support that.

It'll allow you to go through the generation of a page step by step, line by line, understanding what is called, when, from where, ...

Of course, you will not do that for the whole application !
But as your application uses a Framework, there are high chances that all parts of the application work kind of the same way -- which means that really understanding one component should help understanding the other more easily.


As a couple of tools to understand what calls what and how and where, you might want to take a look at :

  • The inclued extension (quoting) : Allows you trace through and dump the hierarchy of file inclusions and class inheritance at runtime
  • Xdebug + KCacheGrind will allow you to generate call-graphs ; XHProf should do the same kind of thing.
  • Using your IDE (Eclipse PDT, Zend Studio, phpStorm, netbeans, ...), ctrl+click on a class/method should bring you to its declaration.


Also note that an application is not only code : it often find very useful to reverse-engineer the database, to generate a diagram of all tables.

If you are lucky, there are foreign keys in your database -- and you'll have links between tables, this way ; which will help you understand how they relate to each other.


You need an IDE. I use netbeans for PHP and it works great. This will allow you to find out where the homeview/home classes are by right clicking and selecting a "find where defined" option or something similar.

You can get a list. This is called the stack. Setting up a debugger like xdebug with the IDE will allow you to do this.


grep is the only thing makes me survive such codez