首页 > 解决方案 > 使用 Xdebug 进行调试时启用 OPcache 时未显示局部变量

问题描述

我启用了 Xdebug 并与 OPcache 一起工作。OPcache 模块首先由 phpfpm 加载(重要)。如果您尝试(从浏览器)调试下面的代码并在局部变量中放置一个断点,您将看到它们没有显示。如果我停用 OPCache,一切都会按预期工作。我正在使用 PhpStorm 2018.2.5 进行调试。

class Foo {
    public $member = 'Some value';

    public function methodFoo() {
        $localFoo = "local2";
    }
}

class Bar {
    public $firstMember = "foo";
    public $secondMember = "bar";

    public function create() {
        $localBar = 10;
        $newFoo = new Foo();
        $newFoo->methodFoo();
        $localBar += 1;
        $this->firstMember = "Other value";
    }

}

$foo = "testing";
$bar = "testing2";

$obj = new Bar();
$obj->create();

我不知道我是否错过了什么或者这是一个错误。多年前在 xdebug 中有一个类似的错误,但它与 OPcache 无关: bug

我留下与我的系统相关的信息和 Xdebug 的日志

phpinfo()
PHP Version => 7.2.10-0ubuntu0.18.04.1

System => Linux pablo-dv6 4.15.0-36-generic #39-Ubuntu SMP Mon Sep 24 16:19:09 UTC 2018 x86_64
Build Date => Sep 13 2018 13:45:02
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /etc/php/7.2/cli
Loaded Configuration File => /etc/php/7.2/cli/php.ini
Scan this dir for additional .ini files => /etc/php/7.2/cli/conf.d
Additional .ini files parsed => /etc/php/7.2/cli/conf.d/10-opcache.ini,
/etc/php/7.2/cli/conf.d/10-pdo.ini,
/etc/php/7.2/cli/conf.d/15-xml.ini,
/etc/php/7.2/cli/conf.d/20-calendar.ini,
/etc/php/7.2/cli/conf.d/20-ctype.ini,
/etc/php/7.2/cli/conf.d/20-curl.ini,
/etc/php/7.2/cli/conf.d/20-dom.ini,
/etc/php/7.2/cli/conf.d/20-exif.ini,
/etc/php/7.2/cli/conf.d/20-fileinfo.ini,
/etc/php/7.2/cli/conf.d/20-ftp.ini,
/etc/php/7.2/cli/conf.d/20-gettext.ini,
/etc/php/7.2/cli/conf.d/20-iconv.ini,
/etc/php/7.2/cli/conf.d/20-intl.ini,
/etc/php/7.2/cli/conf.d/20-json.ini,
/etc/php/7.2/cli/conf.d/20-mbstring.ini,
/etc/php/7.2/cli/conf.d/20-pdo_sqlite.ini,
/etc/php/7.2/cli/conf.d/20-phar.ini,
/etc/php/7.2/cli/conf.d/20-posix.ini,
/etc/php/7.2/cli/conf.d/20-readline.ini,
/etc/php/7.2/cli/conf.d/20-shmop.ini,
/etc/php/7.2/cli/conf.d/20-simplexml.ini,
/etc/php/7.2/cli/conf.d/20-sockets.ini,
/etc/php/7.2/cli/conf.d/20-sqlite3.ini,
/etc/php/7.2/cli/conf.d/20-sysvmsg.ini,
/etc/php/7.2/cli/conf.d/20-sysvsem.ini,
/etc/php/7.2/cli/conf.d/20-sysvshm.ini,
/etc/php/7.2/cli/conf.d/20-tokenizer.ini,
/etc/php/7.2/cli/conf.d/20-wddx.ini,
/etc/php/7.2/cli/conf.d/20-xmlreader.ini,
/etc/php/7.2/cli/conf.d/20-xmlwriter.ini,
/etc/php/7.2/cli/conf.d/20-xsl.ini,
/etc/php/7.2/cli/conf.d/20-zip.ini,
/etc/php/7.2/cli/conf.d/30-xdebug.ini

PHP API => 20170718
PHP Extension => 20170718
Zend Extension => 320170718
Zend Extension Build => API320170718,NTS
PHP Extension Build => API20170718,NTS
Debug Build => no
Thread Safety => disabled
Zend Signal Handling => enabled
Zend Memory Manager => enabled
Zend Multibyte Support => provided by mbstring
IPv6 Support => enabled
DTrace Support => available, disabled

Registered PHP Streams => https, ftps, compress.zlib, php, file, glob, data, http, ftp, phar, zip
Registered Stream Socket Transports => tcp, udp, unix, udg, ssl, tls, tlsv1.0, tlsv1.1, tlsv1.2
Registered Stream Filters => zlib.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk, convert.iconv.*

This program makes use of the Zend Scripting Language Engine:
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies
    with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans


Zend OPcache

Opcode Caching => Disabled
Optimization => Disabled
SHM Cache => Enabled
File Cache => Disabled
Startup Failed => Opcode Caching is disabled for CLI

Directive => Local Value => Master Value
opcache.blacklist_filename => no value => no value
opcache.consistency_checks => 0 => 0
opcache.dups_fix => Off => Off
opcache.enable => On => On
opcache.enable_cli => Off => Off
opcache.enable_file_override => Off => Off
opcache.error_log => no value => no value
opcache.file_cache => no value => no value
opcache.file_cache_consistency_checks => 1 => 1
opcache.file_cache_only => 0 => 0
opcache.file_update_protection => 2 => 2
opcache.force_restart_timeout => 180 => 180
opcache.huge_code_pages => Off => Off
opcache.inherited_hack => On => On
opcache.interned_strings_buffer => 8 => 8
opcache.lockfile_path => /tmp => /tmp
opcache.log_verbosity_level => 1 => 1
opcache.max_accelerated_files => 10000 => 10000
opcache.max_file_size => 0 => 0
opcache.max_wasted_percentage => 5 => 5
opcache.memory_consumption => 128 => 128
opcache.opt_debug_level => 0 => 0
opcache.optimization_level => 0x7FFFBFFF => 0x7FFFBFFF
opcache.preferred_memory_model => no value => no value
opcache.protect_memory => 0 => 0
opcache.restrict_api => no value => no value
opcache.revalidate_freq => 2 => 2
opcache.revalidate_path => Off => Off
opcache.save_comments => 1 => 1
opcache.use_cwd => On => On
opcache.validate_permission => Off => Off
opcache.validate_root => Off => Off
opcache.validate_timestamps => On => On

这部分相关的Xdebug日志(可以看到$localBar没有出现):

<- context_get -i 46 -d 0 -c 0
-> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" command="context_get" transaction_id="46" context="0"><property name="$newFoo" fullname="$newFoo" type="uninitialized"></property><property name="$this" fullname="$this" type="object" classname="Bar" children="1" numchildren="2" page="0" pagesize="100"><property name="firstMember" fullname="$this-&gt;firstMember" facet="public" type="string" size="3" encoding="base64"><![CDATA[Zm9v]]></property><property name="secondMember" fullname="$this-&gt;secondMember" facet="public" type="string" size="3" encoding="base64"><![CDATA[YmFy]]></property></property></response>

欢迎任何帮助,谢谢:)

标签: phpdebuggingphpstormxdebugopcache

解决方案


我回答自己,以防它对某人有用。这是 OPCache 的一个特性。它通过有时删除不需要的变量来优化 PHP 代码。解决方法:调试时禁用OPCache。

sudo phpdismod opcache

并根据你的环境重启 phpfpm 或 apache

sudo service php7.2-fpm restart

或者

sudo service apache2 restart

更多信息在这里


推荐阅读