Printing Out File And Class Information In PHP

18th July 2008 - 2 minutes read time

If you are debugging a PHP application then you might want more information than the values of some current variables. There are a number of built in magic variables that can be used to print out the file name, line number, class and method that the debug statement is printed out on. Here is an example that prints out some information from a class.

class class_test {
function method_test(){
  // the full path to the current file
  print 'File: '.__FILE__.'<br />';
  // print the current line
  print 'Line: '.__LINE__.'<br />';
  // print the current class name
  print 'class: '.__CLASS__.'<br />';
  // print the current method name
  print 'method: '.__METHOD__.'<br />';
  // directory separator of the current
  // system (windows = \ and linux = /)
  print 'Directory separator: '.DIRECTORY_SEPARATOR.'<br />';

$test = new class_test();

This will print out something like the following.

File: C:\Apache Software Foundation\Apache2.2\htdocs\test.php
Line: 10
class: class_test
method: class_test::method_test
Directory separator: \

Add new comment

The content of this field is kept private and will not be shown publicly.