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.

  1. class class_test {
  2.  
  3. function method_test(){
  4. // the full path to the current file
  5. print 'File: '.__FILE__.'<br />';
  6.  
  7. // print the current line
  8. print 'Line: '.__LINE__.'<br />';
  9.  
  10. // print the current class name
  11. print 'class: '.__CLASS__.'<br />';
  12.  
  13. // print the current method name
  14. print 'method: '.__METHOD__.'<br />';
  15.  
  16. // directory separator of the current
  17. // system (windows = \ and linux = /)
  18. print 'Directory separator: '.DIRECTORY_SEPARATOR.'<br />';
  19. }
  20. }
  21.  
  22. $test = new class_test();
  23. $test->method_test();

This will print out something like the following.

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

Add new comment

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