Master PHP Magic Constants: __FUNCTION__, __METHOD__, __CLASS__, ::class and More
This guide explains all nine PHP magic constants, showing how __FUNCTION__, __METHOD__, __CLASS__, __TRAIT__, __NAMESPACE__, __LINE__, __FILE__, __DIR__ and the ::class syntax behave in functions, class methods, traits, inheritance, and Laravel code, with clear examples and output expectations.
Overview
PHP defines nine magic constants that are resolved at compile time, unlike user‑defined constants. Their values depend on where they are used in the code.
__FUNCTION__
The __FUNCTION__ constant returns the name of the function or method in which it appears; outside a function it yields an empty string.
Using in a function
function myFunction(){
echo __FUNCTION__;
}Calling myFunction() prints myFunction.
Using in a class method
class MyClass{
public function myMethod(){
echo __FUNCTION__;
}
}Calling (new MyClass())->myMethod() prints myMethod.
Using in an anonymous function
$myFunction = function(){
echo __FUNCTION__;
};Invoking $myFunction() outputs {closure}.
__METHOD__
The __METHOD__ constant returns the fully qualified class name and method name when used inside a method; otherwise it behaves like __FUNCTION__.
In a class method
namespace App\Utilities;
class MyClass{
public function myMethod(){
echo __METHOD__;
}
}Calling (new MyClass())->myMethod() prints App\Utilities\MyClass::myMethod.
In a parent class method
namespace App\Utilities;
class ParentClass{
public function myMethod(){
echo __METHOD__;
}
}
class ChildClass extends ParentClass{}Calling (new ChildClass())->myMethod() prints App\Utilities\ParentClass::myMethod.
In a function
Inside a plain function, __METHOD__ outputs the same as __FUNCTION__ – the function name.
__CLASS__
The __CLASS__ constant returns the name of the class where it is used; outside a class it returns an empty string.
In a class method
namespace App\Utilities;
class MyClass{
public function myMethod(){
echo __CLASS__;
}
}Calling (new MyClass())->myMethod() prints MyClass.
In a parent class
When used in a parent class, __CLASS__ always returns the parent’s name, even when called from a child.
In a trait
namespace App\Utilities;
trait MyTrait{
public function myMethod(){
echo __CLASS__;
}
}
class MyClass{ use MyTrait; }Calling (new MyClass())->myMethod() prints App\Utilities\MyClass.
::class
The ::class constant yields the fully qualified class name as a string, which is especially handy in Laravel for route definitions and model relationships.
Laravel route example
use App\Http\Controllers\UserController;
Route::get('/users', [UserController::class, 'index']);Laravel model relationship example
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class User extends Model{
public function posts(): HasMany{
return $this->hasMany(Post::class);
}
}__TRAIT__
The __TRAIT__ constant returns the fully qualified name of the trait where it is used; outside a trait it returns an empty string.
namespace App\Utilities;
trait MyTrait{
public function myMethod(){
echo __TRAIT__;
}
}
class MyClass{ use MyTrait; }Calling (new MyClass())->myMethod() prints App\Utilities\MyTrait.
__NAMESPACE__
The __NAMESPACE__ constant returns the current namespace of the file; if the file has no namespace, it returns an empty string.
namespace App\Utilities;
echo __NAMESPACE__;This outputs App\Utilities.
__LINE__
The __LINE__ constant returns the line number where it appears in the source file.
<?php
// An empty line...
echo __LINE__;The above script prints 5 because the echo statement is on line 5.
__FILE__
The __FILE__ constant returns the full path and filename of the file in which it is used.
echo __FILE__;If the script resides at /Users/ashleyallen/my-app/index.php, the output is that full path.
__DIR__
The __DIR__ constant returns the directory of the file. It does not include a trailing slash unless the directory is the root.
echo __DIR__;Running the example in /Users/ashleyallen/my-app/index.php prints /Users/ashleyallen/my-app. The same value can be obtained with dirname(__FILE__).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
