parent
fe99467ea4
commit
6f634726bf
7 changed files with 173 additions and 31 deletions
@ -0,0 +1,52 @@ |
||||
--TEST-- |
||||
Reference-returning functions can forward dynamic and chained calls |
||||
--FILE-- |
||||
<?php |
||||
function &dynamic_source(): mixed |
||||
{ |
||||
static $value = 1; |
||||
return $value; |
||||
} |
||||
|
||||
function &dynamic_forward(string $function): mixed |
||||
{ |
||||
return $function(); |
||||
} |
||||
|
||||
class DynamicRefBox |
||||
{ |
||||
public int $value = 2; |
||||
|
||||
public function &getValue(): mixed |
||||
{ |
||||
return $this->value; |
||||
} |
||||
} |
||||
|
||||
class DynamicRefFactory |
||||
{ |
||||
public function create(): DynamicRefBox |
||||
{ |
||||
return new DynamicRefBox(); |
||||
} |
||||
|
||||
public function &forward(): mixed |
||||
{ |
||||
return $this->create()->getValue(); |
||||
} |
||||
} |
||||
|
||||
function main(): void |
||||
{ |
||||
$dynamic = &dynamic_forward('dynamic_source'); |
||||
$dynamic = 10; |
||||
var_dump(dynamic_source()); |
||||
|
||||
$factory = new DynamicRefFactory(); |
||||
$chained = &$factory->forward(); |
||||
var_dump($chained); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
int(10) |
||||
int(2) |
||||
@ -0,0 +1,39 @@ |
||||
--TEST-- |
||||
Reference-returning static method forwards a late-static-bound reference |
||||
--FILE-- |
||||
<?php |
||||
class RefLsbBase |
||||
{ |
||||
protected static int $value = 1; |
||||
|
||||
protected static function &valueRef(): mixed |
||||
{ |
||||
return static::$value; |
||||
} |
||||
|
||||
public static function &forward(): mixed |
||||
{ |
||||
return static::valueRef(); |
||||
} |
||||
|
||||
public static function value(): int |
||||
{ |
||||
return static::$value; |
||||
} |
||||
} |
||||
|
||||
class RefLsbChild extends RefLsbBase |
||||
{ |
||||
protected static int $value = 2; |
||||
} |
||||
|
||||
function main(): void |
||||
{ |
||||
$value = &RefLsbChild::forward(); |
||||
$value = 20; |
||||
var_dump(RefLsbChild::value(), RefLsbBase::value()); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
int(20) |
||||
int(1) |
||||
@ -0,0 +1,25 @@ |
||||
--TEST-- |
||||
Reference to a typed static property preserves its type constraint |
||||
--FILE-- |
||||
<?php |
||||
use native_types; |
||||
|
||||
class TypedStaticReference |
||||
{ |
||||
public static int $value = 1; |
||||
} |
||||
|
||||
function main(): void |
||||
{ |
||||
$reference = &TypedStaticReference::$value; |
||||
try { |
||||
$reference = 'invalid'; |
||||
} catch (TypeError $error) { |
||||
echo "TypeError\n"; |
||||
} |
||||
var_dump(TypedStaticReference::$value); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
TypeError |
||||
int(1) |
||||
Loading…
Reference in new issue