<?php class Foo { const BAR = 'bar'; public static function getConstantValue() { return self::BAR; } public function getConstant() { return self::BAR; } } $foo = 'Foo'; echo $foo::BAR, '<br />'; echo Foo::BAR, '<br />'; $obj = new Foo(); echo $obj->getConstant(), '<br />'; echo $obj->getConstantValue(), '<br />'; echo Foo::getConstantValue(); class Bar extends Foo { const BAR = 'foo'; public static function getMyConstant() { return self::BAR; } public static function getParentConstant() { return parent::BAR; } } echo Bar::getMyConstant(); // foo echo Bar::getParentConstant(); // bar
Class constants belong to the class itself, not to object instances, and cannot be accessed through object instances
Cannot be modified with public, protected, private, static
Subclasses can override parent classes The constants in the parent class can be called through (parent::)
Since PHP5.3.0, a variable can be used to dynamically call the class. But the value of this variable cannot be the keyword
All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn
Related Article

09 Nov 2024
Distinguishing "Memcache" and "Memcached" in PHPPHP offers two memcached libraries: memcache and memcached. Understanding their differences helps...

19 Nov 2024
Memcache vs Memcached: Choosing the Right PHP Memcached LibraryIntroductionPHP offers two seemingly similar memcached libraries: memcache and...

13 Dec 2024
Linking Static Libraries to Other Static Libraries: A Comprehensive ApproachStatic libraries provide a convenient mechanism to package reusable...

18 Nov 2024
PHP Email Address Validation Libraries UncoveredEmail address validation plays a crucial role in data validation, but creating a...

06 Jul 2016
I have seen many open source projects in the form of class.classname.php, but I have also seen many frameworks in the form of classname.class.php. Where should I place this class? I personally prefer the .class.php form, because in some frameworks, after importing third-party class libraries and specifying class libraries...

12 Nov 2024
Memcache vs. Memcached: Choosing the Right PHP Library for Your Cache NeedsIn the realm of PHP caching libraries, Memcache and Memcached stand out...


Hot Tools

PHP library for dependency injection containers
PHP library for dependency injection containers

A collection of 50 excellent classic PHP algorithms
Classic PHP algorithm, learn excellent ideas and expand your thinking

Small PHP library for optimizing images
Small PHP library for optimizing images
