How to access const class dynamically in PHP?

Say I have a class like this:

class Order {

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

and I would like to take a constant like this:

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

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

I tried:

Order::$status
Order::$$status
+5
source share
1 answer

The function constantdoes this. Syntax

constant('Order::'.$status)

Look at the action .

+12
source

All Articles