5

Let's say I have a class like so:

class Order {

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

and I'd like to grab the constant like so:

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

Is there a way to access the value of the constant, given the name of the constant as a string?

I've tried:

Order::$status
Order::$$status
Andrew
  • 3,332
  • 4
  • 31
  • 37
  • possible duplicate of [Get value of dynamically chosen class constant in PHP](http://stackoverflow.com/questions/6147102/get-value-of-dynamically-chosen-class-constant-in-php) – AbcAeffchen Oct 26 '14 at 23:02

1 Answers1

16

The function constant does this. The syntax is

constant('Order::'.$status)

See it in action.

Jon
  • 428,835
  • 81
  • 738
  • 806