Home >Backend Development >PHP Tutorial >Examples of using PHP class constants

Examples of using PHP class constants

WBOY
WBOYOriginal
2016-07-25 08:59:05838browse
  1. class Foo{
  2. const con_var="The value of a constant attribute cannot be modified
    ";
  3. public function method_a(){
  4. echo (self::con_var);
  5. }
  6. }
  7. echo(Foo::con_var);
  8. $myFoo=new Foo();
  9. echo ($myFoo->method_a());
  10. ?>
Copy code

Note: Constant attributes cannot be accessed using objects, but can only be accessed using classes. Within the class body, you can use "self::constant name", and outside the class body, you can use "class name::constant name".

For information on PHP constants, you can also refer to the following articles: php variables and constants php constants Define and use php constants Learn to understand the usage of constants in php



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn