
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Class Constants
Introduction
PHP allows an identifier in a class to be defined to have a constant value, the one that remains unchanged on a per class basis.To differentiate rom a variable or property wthin class, name of constant is not prefixed with $ symbol and is defined with const qualifier.
Default visibility of a constant is public, although other modifiers may be used in definition. Value of a constant must be certain expression and not a variable, nor a function call/property. value of constant is accessed through class name using scope resolution operator. Inside a method though it can be referred to through self variable
Syntax
class SomeClass{ const CONSTANT = 'constant value'; } echo SomeClass::CONSTANT;
Constant names are case sensitive. Conventionally, name of constant is given in upper case
Class Constant example
This example shows how a Class Constant is defined and accessed
Example
<?php class square{ const PI=M_PI; var $side=5; function area(){ $area=$this->side**2*self::PI; return $area; } } $s1=new square(); echo "PI=". square::PI . "
"; echo "area=" . $s1->area(); ?>
Output
This will produce following result. −
PI=3.1415926535898 area=78.539816339745
Class Constant as expression
In this example, class constant is assigned an expression
Example
<?php const X = 22; const Y=7; class square { const PI=X/Y; var $side=5; function area(){ $area=$this->side**2*self::PI; return $area; } } $s1=new square(); echo "PI=". square::PI . "
"; echo "area=" . $s1->area(); ?>
outer
This will produce following result. −
PI=3.1428571428571 area=78.571428571429
Class constant visibility modifiers
Example
<?php class example { const X=10; private const Y=20; } $s1=new example(); echo "public=". example::X. "
"; echo "private=" . $s1->Y ."
"; echo "private=" . $example::Y ."
"; ?>
Output
This will produce following result. −
1public=10 PHP Notice: Undefined property: example::$Y in line 11 private=PHP Fatal error: Uncaught Error: Cannot access private const example::Y
- Related Articles
- PHP Constants
- PHP Predefined Mathematical Constants
- Display File class constants in Java
- How to implement class constants in TypesScript?
- Which is faster? Constants, Variables or Variable Arrays in PHP?
- PHP Class Abstraction
- PHP Class properties
- PHP Closure class
- PHP Generator class
- PHP WeakReference class
- Difference between C++ string constants and character constants
- Explain abstract class in PHP.
- PHP Basics of Class and Object
- C# TicksPer constants
- Mathematical Constants in Python
