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 Compiler flag haxeJSON does not work for Python · Issue #9203 · HaxeFoundation/haxe · GitHub). More specifically, we’ve overridden class JsonParser and internal function parseNumber.

In the standard library there is a call to String.substr method (haxe/JsonParser.hx at development · HaxeFoundation/haxe · GitHub). 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: haxe/String.hx at 4.1.3 · HaxeFoundation/haxe · GitHub

Thank you!