COMMUNITY

How to handle mixed result from php

haxe-php

(Дмитрий Маликов) #1

in example some php function return boolean false or integer
how to handle it in haxe?


(Aleksandr Kuzmenko) #2

Here is how it’s handled in std: https://github.com/HaxeFoundation/haxe/blob/002502d4a67208dc4226b9479e1b1d3e1cbe9ffa/std/php/Global.hx#L401

static function substr( string:String, start:Int, ?length:Int ) : EitherType<Bool,String>;

(Дмитрий Маликов) #3

how to check result then?


(Aleksandr Kuzmenko) #4

The fast way is php.Global.is_string() and is_bool() methods:

if(php.Global.is_string(result)) {
  trace('string');
  var str:String = result;
} else {
  trace('bool');
  var b:Bool = result;
}

The crossplatform way is to use Std.is(result, String) and Std.is(result, Bool).