Class Constants

It is possible to define constants on a per-class basis remaining the same and unchangeable. The default visibility of class constants is public.

Note:

Class constants can be redefined by a child class. As of PHP 8.1.0, class constants cannot be redefined by a child class if it is defined as final.

It's also possible for interfaces to have constants. Look at the interface documentation for examples.

It's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static).

Note that class constants are allocated once per class, and not for each class instance.

Example #1 Defining and using a constant

<?php
class MyClass
{
    const 
CONSTANT 'constant value';

    function 
showConstant() {
        echo  
self::CONSTANT "\n";
    }
}

echo 
MyClass::CONSTANT "\n";

$classname "MyClass";
echo 
$classname::CONSTANT "\n";

$class = new MyClass();
$class->showConstant();

echo 
$class::CONSTANT."\n";
?>

The special ::class constant allows for fully qualified class name resolution at compile time, this is useful for namespaced classes:

Example #2 Namespaced ::class example

<?php
namespace foo {
    class 
bar {
    }

    echo 
bar::class; // foo\bar
}
?>

Example #3 Class constant expression example

<?php
const ONE 1;
class 
foo {
    const 
TWO ONE 2;
    const 
THREE ONE self::TWO;
    const 
SENTENCE 'The value of THREE is '.self::THREE;
}
?>

Example #4 Class constant visibility modifiers, as of PHP 7.1.0

<?php
class Foo {
    public const 
BAR 'bar';
    private const 
BAZ 'baz';
}
echo 
Foo::BARPHP_EOL;
echo 
Foo::BAZPHP_EOL;
?>

Output of the above example in PHP 7.1:

bar

Fatal error: Uncaught Error: Cannot access private const Foo::BAZ in …

Note:

As of PHP 7.1.0 visibility modifiers are allowed for class constants.

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial