How do I pass constants in a function to be used by another function in 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

链接地址: http://www.djcxy.com/p/47284.html

上一篇: 漂亮的打印JSON不起作用

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