Переглянути джерело

[php] fixed accessing a property with an empty name (fixes #8053)

Alexander Kuzmenko 6 роки тому
батько
коміт
63144f6db9
3 змінених файлів з 28 додано та 4 видалено
  1. 2 2
      std/php/Syntax.hx
  2. 6 0
      std/php/_std/Reflect.hx
  3. 20 2
      tests/unit/src/unit/issues/Issue8053.hx

+ 2 - 2
std/php/Syntax.hx

@@ -152,14 +152,14 @@ extern class Syntax {
         Generates `(object)$value`
     **/
     static inline function object( value:Dynamic ) : StdClass {
-        return codeDeref('(object)({0})', value);
+        return codeDeref('((object)({0}))', value);
     }
 
     /**
         Generates `(array)$value`
     **/
     static inline function array( value:Dynamic ) : NativeArray {
-        return codeDeref('(array)({0})', value);
+        return codeDeref('((array)({0}))', value);
     }
 
     /**

+ 6 - 0
std/php/_std/Reflect.hx

@@ -23,6 +23,8 @@
 import php.Boot;
 import php.Syntax;
 import php.Closure;
+import php.Const;
+import php.NativeAssocArray;
 import haxe.Constraints;
 
 using php.Global;
@@ -47,6 +49,10 @@ using php.Global;
 		}
 		if (!o.is_object()) return null;
 
+		if (field == '' && Const.PHP_VERSION_ID < 70100) {
+			return Syntax.coalesce(Syntax.array(o)[field], null);
+		}
+
 		if (o.property_exists(field)) {
 			return Syntax.field(o, field);
 		}

+ 20 - 2
tests/unit/src/unit/issues/Issue8053.hx

@@ -1,10 +1,28 @@
 package unit.issues;
 
+#if php
+import php.Syntax.*;
+import php.Const.*;
+#end
+
 class Issue8053 extends unit.Test {
 	function test() {
-		var a:Dynamic = { };
 		var b = "";
-		Reflect.setField(a, b, 1);
+
+		#if !php
+		var a:Dynamic = { };
+		#else
+		var a:Dynamic = if(PHP_VERSION_ID < 70100) {
+			//Prior to PHP 7.1.0 there was no way to add a property with an empty name to an existing object.
+			//So, we create an object with an empty property name by transforming an associative array with an empty string index.
+			object(assocDecl({b : 1}));
+		} else {
+			var a = {};
+			Reflect.setField(a, b, 1);
+			a;
+		}
+		#end
+
 		eq(1, Reflect.field(a, b));
 	}
 }