Can you use static constants inside classes in PHP?

PhpClassVariablesSyntaxConstants

Php Problem Overview


I expected the following to work but it doesn't seem to.

<?php

class Patterns
{
	public static const EMAIL = "/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix";
	public static const INT = "/^\d+$/";
	public static const USERNAME = "/^\w+$/";
}

Because it throws this error:

syntax error, unexpected T_CONST, expecting T_VARIABLE

Php Solutions


Solution 1 - Php

You can use const in class like this:

class Patterns {
    const EMAIL = "/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix";
    const INT = "/^\d+$/";
    const USERNAME = "/^\w+$/";
}

And can access USERNAME const like this:

Patterns::USERNAME

Solution 2 - Php

In PHP, static and const are two different things.

const denotes a class constant. They're different than normal variables as they don't have the '$' in front of them, and can't have any visibility modifiers (public, protected, private) before them. Their syntax:

class Test
{
    const INT = "/^\d+$/";
}

Because they're constant, they're immutable.

Static denotes data that is shared between objects of the same class. This data can be modified. An example would be a class that keeps track of how many instances are in play at any one time:

class HowMany
{
    private static $count = 0;

    public function __construct()
    {
        self::$count++;
    }

    public function getCount()
    {
        return self::$count;
    }

    public function __destruct()
    {
        self::$count--;
    }
}

$obj1 = new HowMany();
$obj2 = new HowMany();

echo $obj1->getCount();

unset($obj2);

echo $obj1->getCount();

Solution 3 - Php

They're not static constants, just constants

class Patterns 
{ 
    const EMAIL = "/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix"; 
    const INT = "/^\d+$/"; 
    const USERNAME = "/^\w+$/"; 
} 

echo Patterns::EMAIL;

Solution 4 - Php

Nope class constants can't be labeled static nor assigned visibility.

http://php.net/manual/en/language.oop5.static.php

Solution 5 - Php

You don't need to declare them static or public. Check out the examples in the manual:

http://www.php.net/manual/en/language.oop5.constants.php

Solution 6 - Php

How are you trying to access the constants?

I believe this would work:

echo Patterns::$EMAIL; //shows "/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-...."

If you just declare it as static.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEmanuil RusevView Question on Stackoverflow
Solution 1 - PhpNaveedView Answer on Stackoverflow
Solution 2 - PhpMajor ProductionsView Answer on Stackoverflow
Solution 3 - PhpMark BakerView Answer on Stackoverflow
Solution 4 - PhpSarfrazView Answer on Stackoverflow
Solution 5 - PhpScott SaundersView Answer on Stackoverflow
Solution 6 - PhpChrisView Answer on Stackoverflow