如何在函数中传递常量以供PHP中的另一个函数使用?

I'm trying to pass JSON constants from a handler down to an object that I want to serialize into JSON. When I try the following code:

class AJAXHandler {
    public function getPrettyPrint() {
        $jh = new JSONHandler();
        $jh->getJSON(JSON_PRETTY_PRINT | JSON_FORCE_OBJECT);
    }
}

class JSONHandler {
    protected $id;
    protected $name;

    public function getJSON($json_constants) {
        if (isset($json_constants)) {
            return json_encode(get_object_vars($this), $json_constants);
        } else {
            return json_encode(get_object_vars($this));
        }
    }
}

I get:

Message: Use of undefined constant JSON_PRETTY_PRINT - assumed 'JSON_PRETTY_PRINT'

Is this possible?

You're probably using a PHP version that is lower than version 5.4.

See this answer: https://stackoverflow.com/a/9120871/633098