首页 > 解决方案 > 是否可以跟踪当前的 PHP 函数/方法?

问题描述

我有一个 PHP 类方法,其中包含许多条件语句,如果某些条件为真,它会停止/返回。

对于调试,我想知道哪个条件实际上使函数返回控件。

public function handle_request() {
    if ( ! $this->is_data_writable() ) {
        return;
    }
    if ( ! $this->user_has_permissions() ) {
        return;
    }
    if ( 'active' != $this->object->status ) {
        return;
    }

    // do the stuff
}

我无法控制调用它的函数/方法。

标签: php

解决方案


您可以对其使用日志处理

public function handle_request() {
        if ( ! $this->is_data_writable() ) {
            error_log("is_data_writable", 3, "/var/tmp/my-errors.log");
            return;
        }
        if ( ! $this->user_has_permissions() ) {
            error_log("user_has_permissions", 3, "/var/tmp/my-errors.log");
            return;
        }
        if ( 'active' != $this->object->status ) {
            error_log("not_active", 3, "/var/tmp/my-errors.log");
            return;
        }

        // do the stuff
}

您可以使用自己的错误处理程序自定义错误处理,以便在发生错误或警告或您需要记录的任何内容时为您调用此函数。

https://www.w3schools.com/php/func_error_log.asp


推荐阅读