Introduction
PHP provides 10 magic constants that you can use within your PHP application code.
They are "magic" because they are defined at compile time, unlike regular constants (which you'd define yourself) that are defined at runtime. This means their values can change depending on where they are used in your code.
Admittedly, it's not very often that I use magic constants in my code. The one I'd say I reach for the most is the ::class constant. But I still think they're worth knowing about as they can be useful in certain situations.
In this article, we're going to go through each of the constants and look at what they do.
__FUNCTION__
The __FUNCTION__ magic constant returns the name of the function in which it is used.
If it's used outside a function or method, it will return an empty string.
Using __FUNCTION__ in a function
Let's say you have a function called myFunction:
1function myFunction()2{3 echo __FUNCTION__;4}
Calling the above function would output: myFunction.
Using __FUNCTION__ in a Class Method
Let's say you have a class with a method called myMethod:
1class MyClass2{3 public function myMethod()4 {5 echo __FUNCTION__;6 }7}
Calling the myMethod method would output: myMethod.
Using __FUNCTION__ in an Anonymous Function
Let's say you have an anonymous function:
1$myFunction = function () {2 echo __FUNCTION__;3};
Calling the $myFunction variable would output: {closure}.
__METHOD__
The __METHOD__ magic constant returns the name of the method in which it is used and the class where it's written.
If it's used outside a function or method, it will return an empty string.
Using __METHOD__ in a Class Method
Let's say you have an App\Utilities\MyClass class with a method called myMethod:
1namespace App\Utilities;2 3class MyClass4{5 public function myMethod()6 {7 echo __METHOD__;8 }9}
Calling the myMethod method would output: App\Utilities\MyClass::myMethod.
Using __METHOD__ in a Parent Class Method
The __METHOD__ constant returns the class and method name of where the constant is written. This means if you are using __METHOD__ in a parent class, any child classes will use the parent class' name.
For example, let's say you have a parent class called App\Utilities\ParentClass:
1namespace App\Utilities;2 3class ParentClass4{5 public function myMethod()6 {7 echo __METHOD__;8 }9}
And you have a child class called App\Utilities\ChildClass that extends the App\Utilities\ParentClass class:
1namespace App\Utilities;2 3class ChildClass extends ParentClass4{5 // ...6}
Calling the (new ChildClass())->myMethod() method would output: App\Utilities\ParentClass::myMethod.
Using __METHOD__ in a Function
If you were to call __METHOD__ in a function, the output would be the same as __FUNCTION__. So it would just output the name of the function.
Similarly, if you were to call __METHOD__ in an anonymous function, the output would be the same as __FUNCTION__. So it would just output {closure}.
__CLASS__
The __CLASS__ magic constant returns the name of the class in which it is used.
If it's used outside a class, it will return an empty string.
Using __CLASS__ in a Class Method
Imagine you have the following code:
1namespace App\Utilities;2 3class MyClass4{5 public function myMethod()6 {7 echo __CLASS__;8 }9}
Calling the myMethod method would output: MyClass.
Using __CLASS__ in a Parent Class
Similar to the __METHOD__ magic constant, the __CLASS__ constant will return the name of the class where it is written. This means if you are using __CLASS__ in a parent class the parent class' name will be used rather than the child class'.
For example, let's say you have a parent class called App\Utilities\ParentClass:
1namespace App\Utilities;2 3class ParentClass4{5 public function myMethod()6 {7 echo __CLASS__;8 }9}
And you have a child class called App\Utilities\ChildClass that extends the App\Utilities\ParentClass class:
1namespace App\Utilities;2 3class ChildClass extends ParentClass4{5 // ...6}
Calling the (new ChildClass())->myMethod() method would output: App\Utilities\ParentClass.
Using __CLASS__ in a Trait
If the __CLASS__ constant is used inside a trait, it will return the name of the class that is using the trait.
For example, let's say you have a trait called MyTrait:
1namespace App\Utilities;2 3trait MyTrait4{5 public function myMethod()6 {7 echo __CLASS__;8 }9}
And you have a class called MyClass that uses the MyTrait trait:
1namespace App\Utilities;2 3class MyClass4{5 use MyTrait;6}
Calling the (new MyClass())->myMethod() method would output: App\Utilities\MyClass.
MyClass::class
The ::class magic constant returns the fully qualified class name of a class.
This is the magic constant that I find myself using the most, especially in Laravel applications. It's particularly useful when you're working with class names as strings.
Say you have the following class, App\Utilities\MyClass:
1namespace App\Utilities;2 3class MyClass4{5 // ...6}
Calling App\Utilities\MyClass::class would output: App\Utilities\MyClass.
Using ::class in Laravel
If you're a Laravel developer, you'll have seen this constant used before for things like defining routes and model relationships.
For example, to define a web route in Laravel, you might do something like this in your routes/web.php file:
routes/web.php1use App\Http\Controllers\UserController;2 3Route::get('/users', [UserController::class, 'index']);
Notice how we're using the ::class constant to reference the UserController class which would give us App\Http\Controllers\UserController.
Similarly, when defining relationships in Laravel models, you might do something like this:
app/Models/User.php 1namespace App\Models; 2 3use Illuminate\Database\Eloquent\Model; 4use Illuminate\Database\Eloquent\Relations\HasMany; 5 6class User extends Model 7{ 8 public function posts(): HasMany 9 {10 return $this->hasMany(Post::class);11 }12}
In the above example, we're using the ::class constant to get the fully-qualified name of the Post model class which would give us App\Models\Post.
__TRAIT__
The __TRAIT__ magic constant returns the fully-qualified name of the trait in which it is used.
If it's used outside a trait, it will return an empty string.
For example, say we have the following trait:
1namespace App\Utilities;2 3trait MyTrait4{5 public function myMethod()6 {7 echo __TRAIT__;8 }9}
And we have a class that uses the MyTrait trait:
1namespace App\Utilities;2 3class MyClass4{5 use MyTrait;6}
Calling the (new MyClass())->myMethod() method would output: App\Utilities\MyTrait.
__NAMESPACE__
The __NAMESPACE__ magic constant returns the current namespace in which it is used.
If the file is not in a namespace, it will return an empty string.
For example, let's say we have the following code:
1namespace App\Utilities;2 3echo __NAMESPACE__;
Running the above code would output: App\Utilities.
Similar to the __METHOD__ and __CLASS__ magic constants, the __NAMESPACE__ constant will return the namespace of the file where it is written.
For example, let's say you have a parent class called App\Utilities\ParentClass:
1namespace App\Utilities;2 3class ParentClass4{5 public function myMethod()6 {7 echo __NAMESPACE__;8 }9}
And you have a child class called App\Utilities\Child\ChildClass that extends the App\Utilities\ParentClass class:
1namespace App\Utilities\Child;2 3class ChildClass extends ParentClass4{5 // ...6}
Running the (new ChildClass())->myMethod() method would output: App\Utilities.
__LINE__
The __LINE__ magic constant returns the current line number of the file in which it is used.
For example, let's say we have the following code:
1<?php2 3// An empty line...4 5echo __LINE__;
Running the code above would output 5 because the echo __LINE__; statement is on line 5 of the file.
__FILE__
The __FILE__ magic constant returns the full path and filename of the file in which it is used.
For example, let's say we have the following code inside a file located at /Users/ashleyallen/my-app/index.php
1echo __FILE__;
Running the above code would output /Users/ashleyallen/my-app/index.php.
__DIR__
The __DIR__ magic constant returns the directory of the file in which it is used.
For example, let's say we have the following code inside a file located at /Users/ashleyallen/my-app/index.php
1echo __DIR__;
Running the above code would output /Users/ashleyallen/my-app.
It's worth noting that the __DIR__ constant does not include a trailing slash unless the directory is the root directory.
You can also achieve the same value as __DIR__ by using dirname(__FILE__).
__PROPERTY__
In PHP 8.4, a new __PROPERTY__ magic constant has been introduced. This constant can be used to reference the property name within the property hook.
Let's take a look at an example:
1class User 2{ 3 // ... 4 5 public string $lastName { 6 set(string $name) { 7 echo __PROPERTY__; // lastName 8 } 9 }10 11 public function __construct(12 string $firstName,13 string $lastName,14 ) {15 $this->firstName = $firstName;16 $this->lastName = $lastName;17 }18}
In the code above, we can see that using __PROPERTY__ inside the lastName property's setter will output the property name lastName.
If you to try to access the __PROPERTY__ magic constant outside of a hooked property, an empty string will be returned.
Conclusion
Hopefully, this article has given you an insight into the different magic constants that are available in PHP.
If you enjoyed reading this post, you might be interested in checking out my 220+ page ebook "Battle Ready Laravel" which covers similar topics in more depth.
Or, you might want to check out my other 440+ page ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.
If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter below.
Keep on building awesome stuff! 🚀