Break on property change in Xdebug

╄→гoц情女王★ 提交于 2019-12-01 03:12:00

问题


I'm trying to find out where a certain property of a certain object gets modified. Due to PHP's highly dynamic nature ($o->$prop = $val and such) this is practically impossible to do by simple code analysis. Is there a way to start a debugging session and break at the line where the property gets modified? (Adding a magic __set with a conditional xdebug_break() call to the class might help in simple cases, but if the class or one of its ancestors already has a magic setter, it can get very complicated, so that's not a good solution either.)


回答1:


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




回答2:


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 :)



来源:https://stackoverflow.com/questions/14900850/break-on-property-change-in-xdebug

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