1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00
Files
archived-php-src/ext/opcache/tests/named_parameter_new.phpt
Ilija Tovilo 6173a9a109 VAR|TMP overhaul (GH-20628)
The aim of this PR is twofold:

- Reduce the number of highly similar TMP|VAR handlers
- Avoid ZVAL_DEREF in most of these cases

This is achieved by guaranteeing that all zend_compile_expr() calls, as well as
all other compile calls with BP_VAR_{R,IS}, will result in a TMP variable. This
implies that the result will not contain an IS_INDIRECT or IS_REFERENCE value,
which was mostly already the case, with two exceptions:

- Calls to return-by-reference functions. Because return-by-reference functions
  are quite rare, this is solved by delegating the DEREF to the RETURN_BY_REF
  handler, which will examine the stack to check whether the caller expects a
  VAR or TMP to understand whether the DEREF is needed. Internal functions will
  also need to adjust by calling the zend_return_unwrap_ref() function.

- By-reference assignments, including both $a = &$b, as well as $a = [&$b]. When
  the result of these expressions is used in a BP_VAR_R context, the reference
  is unwrapped via a ZEND_QM_ASSIGN opcode beforehand. This is exceptionally
  rare.

Closes GH-20628
2026-01-31 19:44:56 +01:00

73 lines
1.5 KiB
PHP

--TEST--
Named Parameters are optimized for known constructors
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.optimization_level=-1
opcache.opt_debug_level=0x20000
--SKIPIF--
<?php
if(substr(PHP_OS, 0, 3) == 'WIN') die("skip on windows: Internal classes cannot be optimized");
?>
--EXTENSIONS--
opcache
--FILE--
<?php
final class MyClass
{
private function __construct(
private \Random\Engine $foo,
private int $bar = 0,
) {}
public static function new(int $bar): self
{
$engine = new \Random\Engine\Xoshiro256StarStar(seed: 123);
return new self(foo: $engine, bar: $bar);
}
}
MyClass::new(bar: 1);
?>
--EXPECTF--
$_main:
; (lines=4, args=0, vars=0, tmps=%d)
; (after optimizer)
; %s
0000 INIT_STATIC_METHOD_CALL 1 string("MyClass") string("new")
0001 SEND_VAL int(1) 1
0002 DO_UCALL
0003 RETURN int(1)
MyClass::__construct:
; (lines=7, args=2, vars=2, tmps=%d)
; (after optimizer)
; %s
0000 CV0($foo) = RECV 1
0001 CV1($bar) = RECV_INIT 2 int(0)
0002 ASSIGN_OBJ THIS string("foo")
0003 OP_DATA CV0($foo)
0004 ASSIGN_OBJ THIS string("bar")
0005 OP_DATA CV1($bar)
0006 RETURN null
MyClass::new:
; (lines=10, args=1, vars=2, tmps=%d)
; (after optimizer)
; %s
0000 CV0($bar) = RECV 1
0001 T2 = NEW 1 string("Random\\Engine\\Xoshiro256StarStar")
0002 SEND_VAL int(123) 1
0003 DO_FCALL
0004 CV1($engine) = QM_ASSIGN T2
0005 T2 = NEW 2 (self) (exception)
0006 SEND_VAR CV1($engine) 1
0007 SEND_VAR CV0($bar) 2
0008 DO_FCALL
0009 RETURN T2
LIVE RANGES:
2: 0002 - 0004 (new)
2: 0006 - 0009 (new)