28

I'm looking for a utility similar to gprof that will generate a call graph for PHP code. I'd prefer something that can produce graphical output, or at least text output that can be interpreted by GraphViz or similar, but I'll settle for plain text output.

Does anyone know of any tool that can do this?

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
glomad
  • 5,539
  • 2
  • 24
  • 38

5 Answers5

35

I would definitely try Doxygen. It has support for PHP, and the call graphs and caller graphs it creates have been very useful in exploring "foreign code" for me previously.

Example of doxygen call graph

albert
  • 8,285
  • 3
  • 19
  • 32
Bjarke Freund-Hansen
  • 28,728
  • 25
  • 92
  • 135
4

Not sure there exists anything that can analyse source-code written in PHP to generate that... But there is a possiblity, when you are running the code.

You might want to take a look at the Xdebug extension : it brings profiling to PHP, and and generate callgrind-like files, that can be read with KCacheGrind on Linux.

And this one is able to generate some kind of callgraphs.


It can also be integrated with PHPUNit, to generate code-coverage when running unit-tests
Some time ago, PHPUnit was able to generate some callgraphs with graphviz ; I don't find that option anymore, though :-(


EDIT : it's the first time I hear about it, but there is a project called phpCallGraph that might be able to help you, too... and it seems there is work going on, if I look at it's changelog

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • Thank you - I have tried using Xdebug in the past and unfortunately I cannot get it to work correctly on my target platform, which is an embedded MIPS linux device with some peculiar Apache restrictions (TLS only, one listener process, etc.) – glomad Aug 05 '09 at 20:31
  • Ho... Maybe not so easy, then ^^ You absolutly have no access to a "standard Linux" machine ? Maybe using virtualization ? – Pascal MARTIN Aug 05 '09 at 20:32
  • well, the PHP runs on the platform I mentioned, so the debugger has to run there as well. I will try again someday :) phpCallGraph looks promising though - will check it out. Thanks! – glomad Aug 05 '09 at 20:54
  • You're welcome :-) (could you just add one comment here, when you have tested it, to say how good/bad you think it is ? ) – Pascal MARTIN Aug 05 '09 at 21:58
  • 2
    There hasn't been any work on phpCallGraph since 2009 (date of this answer) – Edson Medina Aug 03 '12 at 13:17
  • See this post for a multi-language static call graph generator, which also supports PHP: https://stackoverflow.com/a/67692928/251947 – Chris Koknat Aug 09 '21 at 19:59
3

As noted already, Doxygen can generate call graphs.

Xdebug can generate function traces. These differ from doxygen's graphs in that they are generated from runtime code, whereas doxygen is generated statically. I don't know of any tools that can turn the function traces into a visual representation, although it shouldn't be that hard to do.

There is also the bytekit extension, which primary function is to show the bytecode that php source code will generate. It can show this in a graph, that is essentially a callgraph.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
troelskn
  • 115,121
  • 27
  • 131
  • 155
  • The bytekit website is now a "website looking at the health issues that are worrying you and your family". – Sam Hobbs Aug 28 '16 at 17:46
3

This is an old topic - but in case anyone finds it useful....

  • kKCachegrind will produce callgraphs shown the actual execution threads.

  • PHPCallGraph is a fairly simple around graphviz which perfroms static analysis

  • There's also my own effort which is similar to PHPCallGraph but adds information about loops/conditional structures and provides a method for adding callbacks to nodes. Some example scripts are bundled with the toolkit - but you can also embed it in your own scripts.

Simon East
  • 55,742
  • 17
  • 139
  • 133
symcbean
  • 47,736
  • 6
  • 59
  • 94
  • As mentioned in other answers, PHPCallGraph seems to be discontinued (there are also many broken links in the site) – AntonioCS Jun 20 '18 at 09:26
2

If you need something interactive, check out the new nWire for PHP. It is an Eclipse plugin which works with either PDT 2.1 and Zend Studio 7.0.

zvikico
  • 9,765
  • 4
  • 38
  • 49