Question

I'm doing an application that uses json. For testeability purposes I made a JsonWrapper:

<?php
    class JsonWrapper {
    private $json_errors;

    public function __construct() {
        $this->json_errors = array(
            JSON_ERROR_DEPTH => ' - Maximum stack depth exceeded',
            JSON_ERROR_STATE_MISMATCH => ' - Underflow or the modes mismatch',
            JSON_ERROR_CTRL_CHAR => ' - Unexpected control character found',
            JSON_ERROR_SYNTAX => ' - Syntax error, malformed JSON',
            JSON_ERROR_UTF8 => ' - Malformed UTF-8 characters, possibly incorrectly encoded'
        );
    }

    public function decode($json, $toAssoc = false) {
        $result = json_decode($json, $toAssoc);

        $errorIndex = json_last_error();
        if (isset($this->json_errors[$errorIndex])) {
            throw new RuntimeException('JSON Error: ' . $this->json_errors[$errorIndex]);
        }

        return $result;
    }
}

As you see, I map all the json error constants with my own error message.

The problem:

  1. If I don't use HipHop this works fine.
  2. If I use HipHop I get this error:

Use of undefined constant JSON_ERROR_DEPTH - assumed 'JSON_ERROR_DEPTH'

I tried to modify the code commenting all the initialization of $json_errors, and test if the error was because the associative array is forbidden in HipHop, but it wasn't that. It kept failing after I put a json constant anywhere on my code.

I also did a test if all the php constants were failing. I test with the 'XML_ERROR_PARTIAL_CHAR' constant, and It didn't fail!

I really don't know what is happening in here and why HipHop hates JSON so much :(

Edit

The main question is:
Why hiphop doesn't understand JSON_ERROR_DEPTH but resolves with no much effort XML_ERROR_PARTIAL_CHAR, if both are defined the same way with the define php function?

json.php: line 170

/**
 * The maximum stack depth has been exceeded.
 * Available since PHP 5.3.0.
 * @link http://php.net/manual/en/json.constants.php
 */
define ('JSON_ERROR_DEPTH', 1);

xml.php: line 559

define ('XML_ERROR_PARTIAL_CHAR', 6);
Was it helpful?

Solution

HipHop doesn't define all the same constants that PHP does. I mean, hiphop is like a re-implementation of PHP, where some things got lost, and other changed.

For example, the PDO constants like PDO::PARAM_INT in HipHop are defined the old way, ie PDO_PARAM_INT.

Also, if I remember correctly, the hiphop implementation for urlencode was like php's raw_urlencode (ie. encoding spaces as %20 as raw_urlencode does in PHP, instead of encoding them to + as PHP's urlencode).

Also HipHop is not implementing a exactly PHP 5.2, because there is some support for namespaces (that came out in PHP 5.3), but they are buggy and not recommended to be use.

In short, you may have to patch some things in your code. For example, you could add to your bootstrap code something like:

<?php
if (!is_defined('JSON_ERROR_NONE')) define('JSON_ERROR_NONE', 0);
// same thing with other JSON_* constants

OTHER TIPS

Ok. Now I know why it wasn't resolving. The problem is the PHP version supported by HipHop!

If you go to the HipHop wikipedia you will see that:
HipHop currently supports PHP version 5.2 and will be updated to support 5.3.

Since all the JSON constants were not available in PHP 5.2, those are not being resolved.

I may be wrong , but i dont think you can do that , your exemple doesnt "compile" in PHP 5.4 with error_reporting(E_ALL); , it issues a notice, meaning that PHP think you forgot quotes around your keys in the array.

You need to declare your constants at the class level with const

class MyClass{
   const MY_CONST = "my value";
}

then call it that way

echo MyClass::MY_CONST ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top