tags:

views:

94

answers:

6

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+$/";
}

I get syntax error, unexpected T_CONST, expecting T_VARIABLE

+1  A: 

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

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

Sarfraz
What's the best alternative in you opinion. If you have to write the example class in a way that acceptable in PHP - how would you do it? Thanks!
Emanuil
@Emanuil: then he would remove "public" and "static" keywords ;-)
zerkms
@Sarfraz: I want to have a class that holds a collection of commonly used regular expressions that all my apps can use.
Emanuil
@Emanuil: Then you should create a public associative array variable to hold those regex.
Sarfraz
wow got a down vote !
Sarfraz
@Sarfraz - It loks as though somebody systematically downvoted almost every answer here about 3 hours ago
Mark Baker
+1 to counteract random downvoting
Scott Saunders
@Mark Baker: There should be a requirement to post a comment after down voting, without a comment no one should be able to down vote but SO has denied this request by the users here :(
Sarfraz
A: 

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.

Chris
it is not a constant in your sample, but a static variable.
zerkms
@zerkms which is why I said if you just declare it as static @mattbasta is that not what he wanted?
Chris
@Chris: you can see what he wanted at the checked answer.
zerkms
+2  A: 

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;
Mark Baker
Any particular reason why I just got a downvote on this?
Mark Baker
Looks like it's not just my response that has been downvoted in the last few minutes either.
Mark Baker
+1 to counteract random downvoting
Scott Saunders
Thank you Scott
Mark Baker
I also got a downvote some hours ago but it is undo now. I think someone was playing with it.
NAVEED
+2  A: 

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

Scott Saunders
+4  A: 

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
NAVEED
+1 to counteract random downvoting
Scott Saunders
A: 

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();
kevinmajor1
Okay, why was I marked down for this?
kevinmajor1
@kevinmajor1z - It looks as though somebody systematically downvoted almost every answer here about 3 hours ago
Mark Baker
+1 to counteract random downvoting
Scott Saunders
Thanks for the upvote help.
kevinmajor1