1

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);
4

3 回答 3

1

HipHop没有定义与 PHP 相同的所有常量。我的意思是,hiphop 就像是对 PHP 的重新实现,其中一些东西丢失了,而另一些则发生了变化。

例如,像PDO::PARAM_INTHipHop 中的 PDO 常量是按旧方式定义的,即PDO_PARAM_INT.

另外,如果我没记错的话,hiphop 的实现urlencode就像 php 的raw_urlencode(即%20raw_urlencode在 PHP 中那样编码空格,而不是将它们编码+为 PHP 的urlencode)。

此外,HipHop 并没有完全实现 PHP 5.2,因为对命名空间有一些支持(在 PHP 5.3 中出现),但它们有问题,不建议使用。

简而言之,您可能需要修补代码中的某些内容。例如,您可以在引导代码中添加如下内容:

<?php
if (!is_defined('JSON_ERROR_NONE')) define('JSON_ERROR_NONE', 0);
// same thing with other JSON_* constants
于 2013-03-24T17:49:14.770 回答
1

好的。现在我知道为什么它没有解决。问题是 HipHop 支持的 PHP 版本!

如果您访问HipHop 维基百科,您会看到:
HipHop currently supports PHP version 5.2 and will be updated to support 5.3.

由于所有 JSON 常量在 PHP 5.2 中都不可用,因此没有得到解决。

于 2013-03-24T16:46:11.940 回答
0

我可能是错的,但我认为你不能这样做,你的例子在 PHP 5.4 中不能“编译” error_reporting(E_ALL);,它会发出通知,这意味着 PHP 认为你忘记了数组中键的引号。

您需要使用 const 在类级别声明常量

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

然后这样称呼它

echo MyClass::MY_CONST ;
于 2013-03-24T15:56:11.210 回答