Hãy cho tôi biết thông tin về từ khóa "traits" trong tài liệu 
 # Ability to use Constants in Traits in PHP 8.2


November 18, 2022
·[PHP](https://www.amitmerchant.com/categories/#PHP)

[Constants](https://www.php.net/manual/en/language.constants.php) are a great way to define values that are not going to change. They are also a great way to define values that are going to be used in multiple places.

- [The problem](https://www.amitmerchant.com/traits-constants-php-82/#the-problem)
- [The solution](https://www.amitmerchant.com/traits-constants-php-82/#the-solution)
- [Limitations](https://www.amitmerchant.com/traits-constants-php-82/#limitations)
- [In conclusion](https://www.amitmerchant.com/traits-constants-php-82/#in-conclusion)

For example, if you have a class that represents a person, you might want to define a constant for the minimum age of a person. This constant can be used in multiple places, such as in the constructor of the class, in a method that checks if the person is an adult, and so on.

## The problem

Up until now, constants could only be defined in classes. This means that if you wanted to use a constant in a trait, you had to define it in a class that uses the trait.

Here’s an example of a trait that utilizes a constant defined in a compounding class (i.e. a class where the trait is used):

```
<?php

trait PersonTrait
{
    public function isAdult(): bool
    {
        return $this->age >= Person::MIN_AGE;
    }
}

class Person
{
    use PersonTrait;

    public const MIN_AGE = 18;

    private int $age;

    public function __construct(int $age)
    {
        $this->age = $age;
    }
}
Copy
```

This is not a big deal, but it can be a bit annoying. You have to define the constant in every class that uses the trait, and you have to make sure that the constant is defined before the trait is used.

## The solution

This is going to be changed in PHP 8.2 with the introduction of constant traits. This means that you can now define constants in traits, and they can be used in classes that use the trait as well as in the trait itself.

Here’s an example of a trait that defines a constant:

```
<?php

trait PersonTrait
{
    public const MIN_AGE = 18;

    public function isAdult(): bool
    {
        return $this->age >= self::MIN_AGE;
    }
}

class Person
{
    use PersonTrait;

    private int $age;

    public function __construct(int $age)
    {
        $this->age = $age;
    }
}

echo Person::MIN_AGE;
// Output: 18

$person = new Person(20);

echo $person->isAdult();
// Output: true
Copy
```

As you can see in the example above, the constant `MIN_AGE` is defined in the trait `PersonTrait`. This constant can be used in the trait itself, as well as in the class `Person`.

You can also define final constants in traits. This means that the constant cannot be overridden in a class that uses the trait unless the class also defines the constant as final.

```
<?php

trait PersonTrait
{
    final public const MIN_AGE = 18;

    public function isAdult(): bool
    {
        return $this->age >= self::MIN_AGE;
    }
}

class Person
{
    use PersonTrait;

    private int $age;

    final public const MIN_AGE = 18;

    public function __construct(int $age)
    {
        $this->age = $age;
    }
}
Copy
```

So, if you have a constant that you want to use in multiple places, you can now define it in a trait and use it in multiple classes.

Apart from this, constants in traits can be used in Enums as well.

```
<?php

trait SuitTrait
{
    public const HEARTS = 'Hearts';
    public const DIAMONDS = 'Diamonds';
    public const CLUBS = 'Clubs';
    public const SPADES = 'Spades';
}

enum Suit: string
{
    use SuitTrait;

    case Hearts = self::HEARTS;
    case Diamonds = self::DIAMONDS;
    case Clubs = self::CLUBS;
    case Spades = self::SPADES;
}
Copy
```

## Limitations

There are a few limitations to how you use constants in traits.

- First of all, you can not directly access a constant defined in a trait from a class that uses the trait. You have to use the `self` keyword to access the constant.

So, this will not work:

```
trait PersonTrait
{
    public const MIN_AGE = 18;
}

class Person
{
    use PersonTrait;

    public function __construct(
        private int $age,
    ) {}

    public function isAdult(): bool
    {
        return $this->age >= PersonTrait::MIN_AGE;
    }
}

$person = new Person(20);

var_dump($person->isAdult());
// PHP Fatal error:  Uncaught Error: Cannot access trait constant PersonTrait::MIN_AGE directly
Copy
```

You can only access the constant from the class that uses the trait if you use the descendant class name, `self`, `static`, `parent`, or its instance.

- You can not override a constant defined in a trait. This means that if you define a constant with the same name in a class that uses the trait and the value/visibility of the constant is different, you will get a fatal error.

```
<?php

trait PersonTrait
{
    public const MIN_AGE = 18;
}

class Person
{
    use PersonTrait;

    private const MIN_AGE = 21;
}

// PHP Fatal error:  Person and PersonTrait define the same constant (MIN_AGE) in the composition of Person. However, the definition differs and is considered incompatible.
Copy
```

- If you declare a final constant in a trait, you can not override it in a class that uses the trait.

```
<?php

trait PersonTrait
{
    final public const MIN_AGE = 18;
}

class Person
{
    use PersonTrait;

    public const MIN_AGE = 21;
}

// PHP Fatal error:  Person and PersonTrait define the same constant (MIN_AGE) in the composition of Person. However, the definition differs and is considered incompatible.
Copy
```

## In conclusion

Constant traits are a great addition to PHP. They allow you to define constants in traits and use them in classes that use the trait. This is especially useful if you have a constant that you want to use in multiple places. This makes it easier to maintain your code, as you only have to define the constant in one place.

[**Previous:** Firefox-only CSS](https://www.amitmerchant.com/firefox-only-css/) [**Next:** Tracking value of number inputs using CSS](https://www.amitmerchant.com/tracking-value-of-an-input-using-css/)

[![PHP 8 in a Nutshell book cover](https://www.amitmerchant.com/images/php-8-in-a-nutshell-large-banner.png)](https://amitmerchant.gumroad.com/l/php8-in-a-nutshell "A no-fluff guide to PHP 8")

[![PHP 8 in a Nutshell book cover](https://www.amitmerchant.com/images/php8-in-a-nutshell-cover.jpg)](https://amitmerchant.gumroad.com/l/php8-in-a-nutshell "A no-fluff guide to PHP 8")

Learn the fundamentals of **PHP 8 (including 8.1, 8.2, 8.3, and 8.4)**, the latest version of PHP, and how to use it today with my new book [PHP 8 in a Nutshell](https://amitmerchant.gumroad.com/l/php8-in-a-nutshell "A no-fluff guide to PHP 8").
It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8.
So, if you're looking for a quick and easy way to PHP 8, this is the book for you.


[Grab your copy now!](https://amitmerchant.gumroad.com/l/php8-in-a-nutshell)[Read a sample](https://www.amitmerchant.com/php-8-in-a-nutshell/sample.pdf)

» Share: [Twitter](https://twitter.com/share?text=Ability+to+use+Constants+in+Traits+in+PHP+8.2%20by%20@amit_merchant&url=https://www.amitmerchant.com/traits-constants-php-82/),
[Facebook](https://www.facebook.com/sharer.php?u=https://www.amitmerchant.com/traits-constants-php-82/),
[Hacker News](https://news.ycombinator.com/submitlink?u=https://www.amitmerchant.com/traits-constants-php-82/&t=Ability%20to%20use%20Constants%20in%20Traits%20in%20PHP%208.2)

Like this article?

[Buy me a coffee](https://buymeacoffee.com/amitmerchant)

[![Caricature of Amit Merchant sketched by a friend](https://www.amitmerchant.com/images/caricature.jpg)](https://twitter.com/amit_merchant "Follow me on Twitter")

👋 Hi there! I'm [Amit](https://twitter.com/amit_merchant). I write articles about all things web development.
You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.


[Become a Sponsor](https://www.amitmerchant.com/become-a-sponsor "Support me via Paypal") [Join my newsletter](https://amitmerchant.substack.com/ "Join my newsletter")

## Comments?

utterances

# 0 Comments _\- powered by_ _[utteranc.es](https://utteranc.es/)_

![@anonymous](<Base64-Image-Removed>)

Nothing to preview


[An open-source desktop playground for PHP](https://www.amitmerchant.com/an-open-source-desktop-playground-for-php/)

[How I create PDF and EPUB for my eBook using PHP](https://www.amitmerchant.com/how-i-create-pdf-and-epub-for-my-ebook/)

[cpx — The npx counterpart of the PHP ecosystem](https://www.amitmerchant.com/cpx-the-npx-counterpart-of-the-php-ecosystem/)

[Never run composer update on your server](https://www.amitmerchant.com/never-run-composer-update-on-your-server/).