COMMUNITY

JSON-related error when compiling Haxe to PHP

Hello,

I think that this is probably not a bug, but my misuse, that’s why I decided to post it here instead of creating a new issue.

The error I am getting when compiling Haxe to PHP is “php.NativeString has no field substr”.

Description:
In our project we had to override standard Haxe implementation of JSON parsing (it was related the case described in https://github.com/HaxeFoundation/haxe/issues/9203). More specifically, we’ve overridden class JsonParser and internal function parseNumber.

In the standard library there is a call to String.substr method (https://github.com/HaxeFoundation/haxe/blob/development/std/haxe/format/JsonParser.hx#L303). But the same code fails to compile when I use it in my overridden class.

I’ve tried providing -D haxeJSON flag to Haxe compiler, but it didn’t help.

BTW, our solution works fine with JS, Java and Python.

Maybe there is some flag that I am missing which allows to disable using of php.NativeString or maybe there is some better solution?

Thanks!

php.NativeString does not have substr field indeed. You can replace it with php.Global.substr for NativeString.

Thanks, it worked!

I ended up using a special condition when compiling to php:

#if !php
var f = str.substr(start, pos - start);
#else
var f = php.Global.substr(str, start, pos - start);
#end

@pbotsman Beware that php.Global.substr() has not the same behaviour as String.substr(). You should use instead php.Global.mb_substr().

See: https://github.com/HaxeFoundation/haxe/blob/4.1.3/std/php/_std/String.hx#L50

Thank you!