Skip to content

Commit

Permalink
Added PHP Magic Constants
Browse files Browse the repository at this point in the history
  • Loading branch information
khianvictorycalderon committed Dec 25, 2024
1 parent 2a0befb commit 65ee6f7
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Magic_Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace MyNamespace;

// Magic constants demo
echo "Current line number: " . __LINE__ . "<br/>"; // Output current line number
echo "Full file path: " . __FILE__ . "<br/>"; // Output the full path to the file
echo "Directory path: " . __DIR__ . "<br/>"; // Output the directory of the file

// Function example using __FUNCTION__
function exampleFunction() {
echo "Function name: " . __FUNCTION__ . "<br/>"; // Output current function's name
}
exampleFunction();

// Class example using __CLASS__
class ExampleClass {
function displayClassName() {
echo "Class name: " . __CLASS__ . "<br/>"; // Output current class's name
}
}
$obj = new ExampleClass();
$obj->displayClassName();

// Method example using __METHOD__
class MethodExample {
public function showMethodName() {
echo "Method name: " . __METHOD__ . "<br/>"; // Output current method's name
}
}
$obj2 = new MethodExample();
$obj2->showMethodName();

// Namespace example using __NAMESPACE__
echo "Current namespace: " . __NAMESPACE__ . "<br/>";
?>

0 comments on commit 65ee6f7

Please sign in to comment.