Break on property change in Xdebug

一笑奈何 提交于 2019-12-01 05:26:50

According to the xdebug documentation, it seems like there should be a way to break on a variable change. http://xdebug.org/docs-dbgp.php#breakpoints

watch: break on write of the variable or address defined by the expression argument

But the source code indicates that the documentation is ahead of its actual functionality: https://github.com/xdebug/xdebug/blob/master/xdebug_handler_dbgp.c#L875

if (strcmp(CMD_OPTION('t'), "watch") == 0) {
        RETURN_RESULT(XG(status), XG(reason), XDEBUG_ERROR_BREAKPOINT_TYPE_NOT_SUPPORTED);
    }

I wasn't actually able to find the string 'watch' anywhere else in the repo, so my assumption is that it's currently unsupported.

There appears to be the bug in Xdebug's bug tracker:

http://bugs.xdebug.org/view.php?id=514

Declare the property that you are trying to debug as private, and create a __set method. Inside of that, you'll be able to find your answer.

class subject extends something {
    private $field;

    public function __set($key, $value) {
        if ($key == 'field') {
            debug_print_backtrace(); exit;
        }

        if (method_exists(get_parent_class($this), '__set')) {
            return parent::__set($key, $value);
        }

        return $this->$key = $value;
    }
}

Edit: this is another phenomenal reason for getters and setters :)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!