PHP 5: const vs static

前端 未结 7 798
长情又很酷
长情又很酷 2020-11-29 16:38

In PHP 5, what is the difference between using const and static?

When is each appropriate? And what role does public, pr

相关标签:
7条回答
  • 2020-11-29 17:35

    So to recap on @Matt great answer:

    • if the property you need should not be changed, then a constant is the the proper choice

    • if the property you need is allowed to be changed, use static instead

    Example:

    class User{
        private static $PASSWORD_SALT = "ASD!@~#asd1";
        ...
    }
    
    class Product{
        const INTEREST = 0.10;
        ...
    }
    

    Edit: It is important to note that PHP 7.1.0 introduced support for specifying the visibility of class constants.

    0 讨论(0)
提交回复
热议问题