View Issue Details

IDProjectCategoryView StatusLast Update
0001644XdebugStep Debuggingpublic2019-03-05 13:27
Reporterdv-ds Assigned To 
PrioritynormalSeveritymajorReproducibilityalways
Status resolvedResolutionno change required 
Product Version2.7.0RC2 
Summary0001644: Cannot evaluate local variable in function
Description

It appears that at least under certain circumstances, it is not possible to evaluate a variable local to a function.

Steps To Reproduce

<?php

function foobar() {
$a = 42;
echo $a; // Place breakpoint here
}

foobar();

// 1. Attempt to evaluate $a by hovering over it (same line as breakpoint), this yields no result. It is also not listed as a local.
// 2. Attempt to evaluate $a in the console. This yields null.

Additional Information

Annotated Xdebug log attached.

Setup:

  • Visual Studio Code, 1.31.1
  • Apache 2.4.37 (Homebrew)
  • PHP 7.3.1 (Homebrew)

I also found an issue that may or may not be related, where evaluating a function argument is only possible when using the console. Would you like a separate bug for this?

TagsNo tags attached.
Operating SystemmacOS
PHP Version7.3.0-7.3.1

Activities

derick

2019-03-04 17:27

administrator   ~0004932

Is opcache loaded and enabled? It's very possible that opcache simply optimised the variable away.

dv-ds

2019-03-04 17:36

reporter   ~0004933

Disabling opcache helps in this case, thanks, then I consider it a PHP bug for now.

For the other case I mentioned, it does not help however. I'll create a separate bug for that tomorrow. It happens at least when using each() - possibly due to the deprecation warning somehow.

dv-ds

2019-03-05 13:05

reporter   ~0004934

Created 1645.

As for this issue:

If you think this a PHP issue and that it might be solved in 7.3.2, please close this issue as I doubt I'll be able to test that theory anytime soon. I'm reluctant to upgrade to 7.3.2 just yet unless something breaks badly for me, as I've had problems with pretty much every PHP upgrade I've done using Homebrew. I can live with disabling the opcache for now.

derick

2019-03-05 13:27

administrator   ~0004935

Hi,

This is not a bug in PHP, but a feature. Opcache tries to make your code run faster, and to do so it optimises your PHP code. In this case, it optimises out the variable. You can see this when you compare the internal opcodes being generated without opcache loaded, against it being loaded:

Without Opcache


derick@singlemalt:~$ php -n -dextension=vld.so -dvld.active=1 /tmp/1644.php

filename: /tmp/1644.php
function name: foobar
number of ops: 3
compiled vars: !0 = $a
line #* E I O op fetch ext return operands

4 0 E > ASSIGN !0, 42
5 1 ECHO !0
6 2 > RETURN null

With opcache

derick@singlemalt:~$ php -n -dzend_extension=opcache.so -dopcache.enable_cli=1 -dextension=vld.so -dvld.active=1 /tmp/1644.php

filename: /tmp/1644.php
function name: foobar
number of ops: 2
compiled vars: none
line #* E I O op fetch ext return operands

5 0 E > ECHO 42
6 1 > RETURN null

The compiled variable !0 / $a is simply missing from the second example.

You can work around this by turning of opcache's optimisations in php.ini:


opcache.optimization_level = 0