Advertisement
bulfaitelo

Magic constants

Oct 31st, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.74 KB | None | 0 0
  1. <?
  2.  
  3.     // Set namespace (works only with PHP 5.3)
  4.     namespace TestProject;
  5.  
  6.     // This prints file full path and name
  7.     echo "This file full path and file name is '" . __FILE__ . "'.\n";
  8.  
  9.     // This prints file full path, without file name
  10.     echo "This file full path is '" . __DIR__ . "'.\n";
  11.  
  12.     // This prints current line number on file
  13.     echo "This is line number " . __LINE__ . ".\n";
  14.  
  15.     // Really simple basic test function
  16.     function test_function_magic_constant() {
  17.         echo "This is from '" . __FUNCTION__ . "' function.\n";
  18.     }
  19.  
  20.     // Prints function and used namespace
  21.     test_function_magic_constant();
  22.  
  23.     // Really simple class for testing magic constants
  24.     class TestMagicConstants {
  25.         // Prints class name
  26.         public function printClassName() {
  27.             echo "This is " . __CLASS__ . " class.\n";
  28.         }
  29.  
  30.         // Prints class and method name
  31.         public function printMethodName() {
  32.             echo "This is " . __METHOD__ . " method.\n";
  33.         }
  34.  
  35.         // Prints function name
  36.         public function printFunction() {
  37.             echo "This is function '" . __FUNCTION__ . "' inside class.\n";
  38.         }
  39.  
  40.         // Prints namespace name (works only with PHP 5.3)
  41.         public function printNamespace() {
  42.             echo "Namespace name is '" . __NAMESPACE__ . "'.\n";
  43.         }
  44.     }
  45.  
  46.     // Create new TestMagicConstants class
  47.     $test_magic_constants = new TestMagicConstants;
  48.  
  49.     // This prints class name and used namespace
  50.     $test_magic_constants->printClassName();
  51.  
  52.     // This prints method name and used namespace
  53.     $test_magic_constants->printMethodName();
  54.  
  55.     // This prints function name inside class and used namespace
  56.     // same as method name, but without class
  57.     $test_magic_constants->printFunction();
  58.  
  59.     // This prints namespace name (works only with PHP 5.3)
  60.     $test_magic_constants->printNamespace();
  61.  
  62. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement