laitimes

Magic constants in PHP

Magic constants in PHP

overview

PHP provides 9 magic constants that you can use in your PHP application code. They are "magical" because they are defined at compile time, unlike regular constants (which you can define yourself) are defined at runtime. This means that their values can change depending on where they are used in the code.

FUNCTION

__FUNCTION__ magic constant returns the name of the function that uses it. If you use it outside of a function or method, it will return an empty string.

Using __FUNCTION__ in Functions

Let's say you have a function called myFunction:

function myFunction()
{
    echo __FUNCTION__;
}           

调用上面的函数将输出:myFunction。

Using __FUNCTION__ in Class Methods

Let's say you have a class that has a method called myMethod:

class MyClass
{
    public function myMethod()
    {
        echo __FUNCTION__;
    }
}           

调用myMethod方法将输出:myMethod。

Using __FUNCTION__ in Anonymous Functions

Let's say you have an anonymous function:

$myFunction = function () {
    echo __FUNCTION__;
};           

调用$myFunction变量将输出:{closure}。

__METHOD__

__METHOD__ magic constant returns the name of the method that used it and the class in which it was written. If you use it outside of a function or method, it will return an empty string.

Using __METHOD__ in Class Methods

假设您有一个App\Utilities\MyClass类,其方法名为myMethod:

namespace App\Utilities;
 
class MyClass
{
    public function myMethod()
    {
        echo __METHOD__;
    }
}           

调用myMethod方法将输出:App\Utilities\MyClass::myMethod。

Using __METHOD__ in the Parent Class Method

Constants return the class and method names in which the constants are located. This means that if you use __METHOD__ in a parent class, any child classes will use the name of the parent class.

例如,假设你有一个名为App\Utilities\ParentClass的父类:

namespace App\Utilities;
 
class ParentClass
{
    public function myMethod()
    {
        echo __METHOD__;
    }
}           

你有一个名为App\Utilities\ChildClass的子类,它扩展了App\Utilities\ParentClass类:

namespace App\Utilities;
 
class ChildClass extends ParentClass
{
    // ...
}           

调用(new ChildClass())->myMethod()方法将输出:App\Utilities\ParentClass::myMethod。

Using __METHOD__ in Functions

If you call __METHOD__ in a function, the output will be the same as __FUNCTION__. So it will only output the name of the function.

Similarly, if you call __METHOD__ in an anonymous function, the output will be the same as __FUNCTION__. So it will only output {closure}.

__CLASS__

__CLASS__ magic constant returns the name of the class that uses it. If you use it outside of the class, it will return an empty string.

Using __CLASS__ in Class Methods

Let's say you have the following code:

namespace App\Utilities;
 
class MyClass
{
    public function myMethod()
    {
        echo __CLASS__;
    }
}           

调用myMethod方法将输出:MyClass。

Using __CLASS__ in Parent Class

Similar to __METHOD__ magic constant, __CLASS__ constant returns the name of the class in which it was written. This means that if you use __CLASS__ in a parent class, then the name of the parent class will be used, not the name of the child class.

例如,假设你有一个名为App\Utilities\ParentClass的父类:

namespace App\Utilities;
 
class ParentClass
{
    public function myMethod()
    {
        echo __CLASS__;
    }
}           

你有一个名为App\Utilities\ChildClass的子类,它扩展了App\Utilities\ParentClass类:

namespace App\Utilities;
 
class ChildClass extends ParentClass
{
    // ...
}           

调用(new ChildClass())->myMethod()方法将输出:App\Utilities\ParentClass。

Using __CLASS__ in Trait

If a __CLASS__ constant is used in a trait, it will return the name of the class that uses the trait.

For example, let's say you have a trait called MyTrait:

namespace App\Utilities;
 
trait MyTrait
{
    public function myMethod()
    {
        echo __CLASS__;
    }
}           

You have a class called MyClass that uses MyTraittrait:

namespace App\Utilities;
 
class MyClass
{
    use MyTrait;
}           

调用(new MyClass())->myMethod()方法将输出:App\Utilities\MyClass。

::class

::class magic constant returns the fully qualified class name of the class.

This is the magic constant that I find myself using the most, especially in the Laravel app. It's especially useful when you're using class names in string form.

假设你有下面的类,App\Utilities\MyClass:

namespace App\Utilities;
 
class MyClass
{
    // ...
}           

调用App\Utilities\MyClass::class将输出:App\Utilities\MyClass。

Use ::class in Laravel

If you're a Laravel developer, you'll see that this constant was previously used to define routing and model relationships.

For example, to define a web route in Laravel, you can do this in your routes/web.php file:

use App\Http\Controllers\UserController;
 
Route::get('/users', [UserController::class, 'index']);           

Notice how we use the ::class constant to refer to the UserController class, which will give us App\Http\Controllers\UserController. Similarly, when defining a relationship in a Laravel model, you can do this:

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);
    }
}           

In the example above, we use the ::class constant to get the fully qualified name of the Post model class, which will give us App\Models\Post.

TRAIT

__TRAIT__magic constant returns the fully qualified name of the trait that uses it. If used outside of a trait, it will return an empty string.

For example, suppose we have the following traits:

namespace App\Utilities;
 
trait MyTrait
{
    public function myMethod()
    {
        echo __TRAIT__;
    }
}           

We have a class that uses MyTraittrait:

namespace App\Utilities;
 
class MyClass
{
    use MyTrait;
}           

调用(new MyClass())->myMethod()方法将输出:App\Utilities\MyTrait。

NAMESPACE

__NAMESPACE__ magic constant returns the namespace that is currently using it. If the file is not in the namespace, it will return an empty string. For example, let's say we have the following code:

namespace App\Utilities;
 
echo __NAMESPACE__;           

运行上面的代码会输出:App\Utilities。

Similar to __METHOD__ and __CLASS__ magic constants, __NAMESPACE__ constants return the namespace of the file written to it.

例如,假设你有一个名为App\Utilities\ParentClass的父类:

namespace App\Utilities;
 
class ParentClass
{
    public function myMethod()
    {
        echo __NAMESPACE__;
    }
}           

你有一个名为App\Utilities\Child\ChildClass的子类,它扩展了App\Utilities\ParentClass类:

namespace App\Utilities\Child;
 
class ChildClass extends ParentClass
{
    // ...
}           

运行(new ChildClass())->myMethod()方法将输出:App\Utilities。

LINE

__LINE__ magic constant returns the current line number of the file that uses it.

For example, let's say we have the following code:

<?php
 
// An empty line...
 
echo __LINE__;           

Running the above code will output 5 because echo __LINE__; The statement is located on line 5 of the file.

FILE

__FILE__ magic constant returns the full path and filename of the file that uses it.

例如,假设我们在位于/Users/ashleyallen/my-app/index.php的文件中有以下代码

echo __FILE__;           

运行上面的代码将输出/Users/ashleyallen/my-app/index.php。

YOU

__DIR__ magic constant returns the directory of the file that uses it.

例如,假设我们在位于/Users/ashleyallen/my-app/index.php的文件中有以下代码

echo __DIR__;           

运行上面的代码将输出/Users/ashleyallen/my-app。

It's worth noting that unless the directory is the root, __DIR__ constant doesn't contain trailing slashes.

You can also use dirname(__FILE__) to achieve the same value as __DIR__.

Read on