Break changing properties in Xdebug

I'm trying to figure out where a certain property of a certain object changes. Due to the very dynamic nature of PHP ( $o->$prop = $val

etc.), this is almost impossible to do with simple code analysis. Is there a way to start a debugging session and break at the line where the property changes? (Adding a __set

conditional magic xdebug_break()

to a class can help in simple cases, but if the class or one of its ancestors already has a magic setter, it can get very complex, so there won't be a good solution either.)

+3


source to share


2 answers


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

watch: break in writing a variable or address specified by an 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 really couldn't find the "watch" line anywhere else in the repo, so my guess is that it is currently not supported.

There seems to be a bug in the Xdebug bug tracker:

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

+7


source


Declare the property you are trying to debug like private

and create a method __set

. Inside this, you should 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 :)

+6


source







All Articles