0

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?

NobleUplift
  • 5,631
  • 8
  • 45
  • 87
  • Where is `JSON_PRETTY_PRINT` defined? – TheWolf Oct 10 '13 at 20:18
  • What you're doing should work, but the constants are only present in PHP 5.4+. I'd guess you're using PHP 5.3 or lower –  Oct 10 '13 at 20:21
  • 1
    @TheWolf JSON_PRETTY_PRINT and other JSON constants are available in PHP5.4+ - see [here](http://php.net/manual/en/function.json-encode.php) –  Oct 10 '13 at 20:22
  • Well that's what I get for using the OS X default web server for testing. Also what I get for trying to read the documentation with lack of sleep... or while I was sleeping. I can't remember which. – NobleUplift Oct 10 '13 at 20:51

1 Answers1

3

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

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

Community
  • 1
  • 1
silkfire
  • 24,585
  • 15
  • 82
  • 105