diff --git a/phpunit/code/override_byref_return_abstract_dropped.php b/phpunit/code/override_byref_return_abstract_dropped.php new file mode 100644 index 00000000..b5a56dec --- /dev/null +++ b/phpunit/code/override_byref_return_abstract_dropped.php @@ -0,0 +1,16 @@ +exec( + 'Declaration of `ConcreteByRefReturn::value()` must be compatible with ' + . '`AbstractByRefReturn::value()`', + 'override_byref_return_abstract_dropped.php', + ); + } + + public function testInterfaceImplementationCannotDropByRefReturn(): void + { + $this->exec( + 'Declaration of `ByRefReturnImplementation::value()` must be compatible with ' + . '`ByRefReturnContract::value()`', + 'override_byref_return_interface_dropped.php', + ); + } } diff --git a/tests/compiler/ref/abstract-interface-add-byref-return.phpt b/tests/compiler/ref/abstract-interface-add-byref-return.phpt new file mode 100644 index 00000000..b0598177 --- /dev/null +++ b/tests/compiler/ref/abstract-interface-add-byref-return.phpt @@ -0,0 +1,47 @@ +--TEST-- +Abstract and interface implementations may add a by-reference return +--FILE-- +abstractStored; + } + + public function &interfaceValue(): int + { + return $this->interfaceStored; + } +} + +function main(): void +{ + $source = new ReferenceValueSource(); + + $abstractAlias =& $source->abstractValue(); + $abstractAlias = 50; + var_dump($source->abstractValue()); + + $interfaceAlias =& $source->interfaceValue(); + $interfaceAlias = 70; + var_dump($source->interfaceValue()); +} +?> +--EXPECT-- +int(50) +int(70) diff --git a/tests/compiler/ref/override-add-byref-return.phpt b/tests/compiler/ref/override-add-byref-return.phpt new file mode 100644 index 00000000..46f273d7 --- /dev/null +++ b/tests/compiler/ref/override-add-byref-return.phpt @@ -0,0 +1,71 @@ +--TEST-- +An override may add a by-reference return and preserve alias semantics +--FILE-- +stored; + } +} + +class StaticByRefReturnParent +{ + public static function value(): int + { + return -1; + } +} + +final class StaticByRefReturnChild extends StaticByRefReturnParent +{ + private static int $stored = 20; + + public static function &value(): int + { + return self::$stored; + } +} + +function readByRefReturnParent(ByRefReturnParent $value): int +{ + return $value->value(); +} + +function main(): void +{ + $child = new ByRefReturnChild(); + var_dump(readByRefReturnParent($child)); + + $instanceAlias =& $child->value(); + $instanceAlias = 42; + var_dump($child->value()); + var_dump(readByRefReturnParent($child)); + + $instanceCopy = $child->value(); + $instanceCopy = 99; + var_dump($child->value()); + + $staticAlias =& StaticByRefReturnChild::value(); + $staticAlias = 84; + var_dump(StaticByRefReturnChild::value()); +} +?> +--EXPECT-- +int(10) +int(42) +int(42) +int(42) +int(84)