【问题标题】:How to access a class const dynamically in PHP?如何在 PHP 中动态访问类 const?
【发布时间】:2012-02-12 17:56:01
【问题描述】:

假设我有这样的课程:

class Order {

    const STATUS_INITIALIZED = 'initialized';
    const STATUS_ORDERED = 'ordered';
}

我想像这样获取常量:

$status = $_GET['status']; // ?status=STATUS_ORDERED

如果给定常量名作为字符串,有没有办法访问常量的值?

我试过了:

Order::$status
Order::$$status

【问题讨论】:

标签: php get constants


【解决方案1】:

函数constant 执行此操作。语法是

constant('Order::'.$status)

See it in action.

【讨论】:

  • @Andrew:很高兴为您提供帮助。干杯!
猜你喜欢
  • 2018-06-25
  • 1970-01-01
  • 2018-02-25
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多