That's not supposed to work, so it's actually a bug in the PHP implementation of typed casts. An empty object does not implement any interfaces and the cast is thus invalid. Interfaces are explicitly contracts, that are not only present at runtime, but also implemented at compiletime.
else throw '$x is not $d';
I believe some platforms actually implement it along these lines. The point is, the runtime type check (i.e. `Std.is`) will not return true, and you will thus get the error.
However, you can use anonymous types instead (the compiletime equivalent of ducktyping if you will):
var data: { field1:String, field2: String, field3: String } = untyped {}; //or `cast {}` if you wish
And you can use a typedef:
typedef TableName = { field1:String, field2: String, field3: String }
Thus allowing you to do:
var data:TableName = untyped {};
But the following two variants are both better, because they don't involve slapping the type system in the face:
1. Initialize directly:
var data:TableName = { field1: "value1", field2: "value2", field3: "value3" };
2. Use optional fields:
typedef TableName = { ?field1:String, ?field2: String, ?field3: String }
var data:TableName = {};
Please note the difference: In the first example you establish an explicit contract, that a TableName *must* have these fields defined and you thus honor it. In the second example you say the fields are optional and therefore the compiler will let you add them later as needed.
Regards,
Juraj