我如何连接一个常量和一个变量,并将其存储在类常量与PHP? [英] How can I concatenate a constant and a variable and store it in a class constant with PHP?

查看:285
本文介绍了我如何连接一个常量和一个变量,并将其存储在类常量与PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class My_class
{
    const STATUS_ERROR = 0;
    const STATUS_OK = 1;
    const DB_TABLE = TABLE_PREFIX . 'class_table';
}

两个状态常量工作正常,可以在类方法中访问 self :: STATUS_ERROR self :: STATUS_OK 很好。

The two status consts work fine and can be accessed within class methods as self::STATUS_ERROR and self::STATUS_OK just fine.

问题是如何停止

解析错误:语法错误,意外的'。',期望','或' ;'in /home/sub/sub/directory/script.php

推荐答案

t。常数是常数。

您可以使用静态属性。

class My_Class {
  public static $DB_TABLE;
}
My_Class::$DB_TABLE = TABLE_PREFIX . 'class_table';

您不能在声明中执行,因此您可能更喜欢使用静态方法。 p>

You can't do it within the declaration, so you might prefer a static method instead.

class My_Class {
  public static function dbTable() {
    return TABLE_PREFIX . 'class_table';
  }
}

这篇关于我如何连接一个常量和一个变量,并将其存储在类常量与PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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