可以在PHP中使用静态常量吗? [英] Can you use static constants in PHP?

查看:69
本文介绍了可以在PHP中使用静态常量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望以下内容能起作用,但似乎没有。

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

我收到语法错误,意外的T_CONST,期望T_VARIABLE

推荐答案

您可以在此类中使用const:

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

并且可以访问 USERNAME const是这样的:

And can access USERNAME const like this:

Patterns::USERNAME

这篇关于可以在PHP中使用静态常量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆