Get file name/line number from which function was called
<p><strong>A little background first:</strong></p>

<p>I am picking through one of the PHP-based systems that we use internally in an effort to understand some of the code (it is a project I have inheritted the maintenance of) largely because it is producing log files of 30-40MB a day, and the log files are filled with debugging information that under normal circumstances is pointless. The system works (at least, 98% of it does) and most of the information that is being logged would only be useful if it wasn't working.</p>

<p>It seems that all logging is done by a set of procedural-style functions, and there is no logging level control - in other words, all log messages are sent to the same set of functions with no indication as to whether they are errors, warnings, informational, debug-level etc etc. So I have decided to replace these logging functions with a set of logging objects that I have used a few times before, with great success.</p>

<p>It is rather difficult to spin this up in a test environment. The system was developed on the live servers, and relies on a huge amount of database data being available (and writable) which would be very difficult to create a copy of for testing. It collects a lot of information from remote systems (up to 10000 messages a minute) and much of the testing would require this constant feed of information. I know this is not ideal, but trust that it is the situation and that I cannot do anything about it (at least not in a reasonable period of time) - I am telling you this because I don't want any suggestions involving using PHP's error functionality.</p>

<p><strong>Now, the actual problem:</strong></p>

<p>I can easily do a find/replace on calls to the logging functions to implement the OO logging. However, there are several places that I am aware of, and probably several more that I am not aware of, where the functions are called "dynamically" i.e. something like this has been done (simplified example):</p>

<pre><code>$funcName = $someRandomVar."_log_entry";
$funcName("Some logging message");
</code></pre>

<p>...so a simple find replace will not work completely.  So to work around this problem (in the interim) I am planning to leave the old functions defined, to avoid the inevitable <code>Fatal Error - Call to undefined function...</code> errors. I will redefine the functions something likes this:</p>

<pre><code>function core_log_entry ($msg) {
  global $coreLogger;
  $coreLogger-&gt;warn("DEPRECATED LOGGING FUNCTION USED: $msg");
}
</code></pre>

<p>But what I would really like to do is get the filename and line number where the deprecated function was called into the message as well. Is there any way to get this dynamically?</p>

<p>I know there are the <code>__FILE__</code> and <code>__LINE__</code> constants, but these will hold the values pertaining to the logging function definition, not the location from which it was called. I also know I could pass <code>__FILE__</code> and <code>__LINE__</code> as arguments, but this doesn't help since I would still have to pick through the entire code to do this - it can't be done with find/replace. Also, the extra condition here - it has to work on PHP 4.3.10 (for reasons I don't want to get into for fear that I might just break down in tears).</p>

<p><strong>Side question</strong> - I came across the following in one of the files, has it ever been legal to do this? It certainly doesn't work on any PHP version I have available...</p>

<pre><code>// I know you are allowed to do this...
$method = 'methodName';
$object-&gt;$method();

// ...but has this ever been allowed?
$method = 'object-&gt;methodName';
$method();
</code></pre>
