Skip to content

Instantly share code, notes, and snippets.

@adam-stokes
Forked from RealyUniqueName/Json.hx
Created February 10, 2022 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adam-stokes/17847481e7c4204121aea123350cc99c to your computer and use it in GitHub Desktop.
Save adam-stokes/17847481e7c4204121aea123350cc99c to your computer and use it in GitHub Desktop.
Sample PHP externs for Haxe
-main Main
-php bin
#this flag is required if you're using Haxe 3.4
-D php7
{
"require": {
"zendframework/zendframework": "^3.0"
}
}
//You can use any package name here.
//It's not required to match the namespace of php file.
package;
import php.NativeStructArray;
typedef EncodingOptions = {
?prettyPrint:Bool,
?enableJsonExprFinder:Bool,
}
@:native('Zend\\Json\\Json')
extern class Json {
@:phpClassConst static var TYPE_ARRAY(default,never):Int;
@:phpClassConst static var TYPE_OBJECT(default,never):Int;
static var useBuiltinEncoderDecoder:Bool;
static function decode(encodedValue:String, ?objectDecodeType:Int):Any;
static function encode(valueToEncode:Any, ?cycleCheck:Bool, ?options:NativeStructArray<EncodingOptions>):String;
static function prettyPrint(json:String, ?options:NativeStructArray<{?indent:String}>):String;
}
import php.Global;
import php.Const;
import php.NativeArray;
class Main {
static function __init__() {
//Assuming you're using Composer for the dependency management
//and the `vendor` directory is located in the root of your project.
Global.require_once(Const.__DIR__ + '/../../vendor/autoload.php');
}
static function main() {
var obj:NativeArray = Json.decode('{"field":"value"}', Json.TYPE_ARRAY);
trace(obj['field']);
var str = Json.encode(obj, false, {prettyPrint:true});
trace(str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment