title: How to get the name of the script calling a function?
body: <p>I want to write a very generic function for logging :</p>
  
  <pre><code>function msglog($type, $message) {
  echo date("r") . " " . __FILE__ . " - $type - $message" . PHP_EOL;
  }
  </code></pre>
  
  <p>I am going to use it almost everywhere in my code which will be dispatched in several php scripts <code>include</code>. As shown here, wherever the function is called, <code>__FILE__</code> is always valued with the main script filename.</p>
  
  <p>Is there a way I can get in the function itself the name of the caller script without having to pass it as a parameter ? </p>
  
  <p><strong>EDIT1 - it is not a duplicate</strong></p>
  
  <p>Eventhough the aim is to retrieve caller information, just like some other questions asked and the solution is the same. Knowing the answer make it obvious they are connected. My question had another "scope".  It is relative to includes and script filename retrieval, not the function name in an unknown context.</p>
  
  <p><strong>EDIT2 - The answer</strong></p>
  
  <p>Yes, the <code>debug_backtrace()</code> function is the answer.</p>
  
  <p>Here is the solution I was looking for :</p>
  
  <pre><code>function msglog( $type, $message) {
      $caller = debug_backtrace();
      echo date("r") . $caller[0]["file"] . " - $type - $message" . PHP_EOL;
  }
  </code></pre>