1--TEST-- 2Union types with multiple classes 3--FILE-- 4<?php 5 6class Test { 7 public X|Y|Z|int $prop; 8 public function method(X|Y|Z|int $arg): X|Y|Z|int { 9 return $arg; 10 } 11} 12 13// Check that nothing here triggers autoloading. 14spl_autoload_register(function($class) { 15 echo "Loading $class\n"; 16}); 17 18$test = new Test; 19 20$test->prop = 42; 21var_dump($test->prop); 22var_dump($test->method(42)); 23 24$test->prop = "42"; 25var_dump($test->prop); 26var_dump($test->method("42")); 27 28try { 29 $test->prop = new stdClass; 30} catch (TypeError $e) { 31 echo $e->getMessage(), "\n"; 32} 33 34try { 35 $test->method(new stdClass); 36} catch (TypeError $e) { 37 echo $e->getMessage(), "\n"; 38} 39 40if (true) { 41 class X {} 42} 43 44$test->prop = new X; 45var_dump($test->prop); 46var_dump($test->method(new X)); 47 48if (true) { 49 class Z {} 50} 51 52$test->prop = new Z; 53var_dump($test->prop); 54var_dump($test->method(new Z)); 55 56if (true) { 57 class Y {} 58} 59 60$test->prop = new Y; 61var_dump($test->prop); 62var_dump($test->method(new Y)); 63 64?> 65--EXPECTF-- 66int(42) 67int(42) 68int(42) 69int(42) 70Cannot assign stdClass to property Test::$prop of type X|Y|Z|int 71Test::method(): Argument #1 ($arg) must be of type X|Y|Z|int, stdClass given, called in %s on line %d 72object(X)#4 (0) { 73} 74object(X)#6 (0) { 75} 76object(Z)#6 (0) { 77} 78object(Z)#4 (0) { 79} 80object(Y)#4 (0) { 81} 82object(Y)#6 (0) { 83} 84