How to debug functions within encoded PHP files.

Sometimes you just don't have the ability to see what files are including what. Such as the case with a closed-source system I'm working with on a support level.

Sadly, these people are not very good with their code, and are consistently duplicating functions with the same name in different files, so if I need functions from one, and I include the other, it's going to die with the namespace colission.

This prompted me to write this simple little PHP4 and PHP5 compatible 'sniffer', which, when executed, will tell you wether or not a function is available within an included file, and any files it may include.

It also has skeletal abilities to change the built in functions, and call your own, which can then pass all data to the native function.. This is done for include() in PHP5. However, since this development system is still on PHP4, I've only got get_included_files() to keep me warm, but that's fine.

<?php
// FunctionSniffer v0.1 by Shawn Holwegner <shawnospamn@holwegner.com>
// --
// This software is not public domain, however you may use it, and modify
// it for your own use, but please do not distribute modified copies.
if (version_compare(phpversion(), "5.0.0", "gt")) {
  if ((function_exists('rename_function') \
  && function_exists('override_function'))) {
    rename_function('include', 'std_include');
    override_function('include', '$string', \
    'return override_include($string);');
  }
}


if (file_exists($argv[1]))
  @include_once($argv[1]);
if($argv[2])
  if (function_exists($argv[2])) {
    echo $argv[1] . " has function " . $argv[2] . "().\n";
    $files="";
    foreach (get_included_files() as $filenames) {
      if ($filenames != __FILE__)
      $files.="\t" . $filenames . "\n";
    }
    if (!empty($files))
      echo "It asked for files:\n$files\n";
  }
exit;

function override_include($string) {
  echo "override_include(): We're including $string\n";
  return std_include($string);
}
?>
Here's how it works:
%php ~/code/php/misc/functionSniffer.php helper.php my_db_query
helper.php has function my_db_query().
It asked for files:
        /home/www/protected.com/productname/includes/help.php
        /home/www/protected.com/productname/includes/head.php
        /home/www/protected.com/productname/includes/core_functions.php
        /home/www/protected.com/productname/includes/database.php
        /home/www/protected.com/productname/includes/config.php
        /home/www/protected.com/productname/includes/config_override.php
        /home/www/protected.com/productname/includes/admin.php
With PHP 5, it will even tell you realtime as things are include()'d, but that's somewhat of a 'to be developed' system, which requires pecl's apd loaded as a zend extension, which doesn't work with Zend Encoder.