无法从类中的函数访问类参数值

时间:2015-06-06 15:53:44

标签: php

尝试从函数print_debug_info()中的类访问对象参数值并获取运行时错误。

class debugInfoAPICall
{
    var $uri         = "";
    var $request     = "";
    var $response    = "";
    var $signature   = "";
    var $timestamp   = "";
    var $exectime    = -1;
    var $httpcode    = -1;

    function __construct($uri, $request, $response, $signature, $timestamp, $exectime, $httpcode) {
        $this->uri          = $uri;
        $this->request      = $request;
        $this->response     = $response;
        $this->signature    = $signature;
        $this->timestamp    = $timestamp;
        $this->exectime     = $exectime;
        $this->httpcode     = $httpcode;
    }

    function print_debug_info (){

        echo "<b>API:</b> <font class=\"text-info\">" . $this->$uri . "</font> <br>";
        if ($this->httpcode != 200){
            echo "<span class=\"glyphicon glyphicon-alert\" style=\"color:#FF8C00\"></span> <b>Response Time:</b> <font class=\"text-danger\">" . $this->httpcode . "</font> <br>";
        }else{
            echo "<b>HTTP Code:</b> " . $this->httpcode . " <br>";
        }
        if ($this->exectime > 500){
            echo "<span class=\"glyphicon glyphicon-alert\" style=\"color:#FF8C00\"></span> <b>Response Time:</b> <font class=\"text-danger\">" . $this->$exectime . " ms</font> <br>";
        }else{
            echo "<b>Response Time:</b> " . $this->exectime . " ms<br>";
        }
        echo "<b>Timestamp:</b> " . $this->timestamp . " <br>";
        echo "<b>Signature:</b> " . $this->signature . " <br><br>";
        echo "<b>Request Data JSON:</b> <pre>" . $this->request . "</pre> <br>";
        echo "<b>Response Data JSON:</b> <pre>" . $this->response . "</pre> <br>";
    }   
}

从课外访问时,获取对象的参数值并打印它们没有问题,因此以下工作正常。

foreach ($apiCallLog as &$value) {

            echo "<b>URI:</b> " . $value->uri . " <br>";
            if ($value->httpcode != 200){
                echo "<span class=\"glyphicon glyphicon-alert\" style=\"color:#FF8C00\"></span> <b>Response Time:</b> <font class=\"text-danger\">" . $value->httpcode . "</font> <br>";
            }else{
                echo "<b>HTTP Code:</b> " . $value->httpcode . " <br>";
            }
            if ($value->exectime > 500){
                echo "<span class=\"glyphicon glyphicon-alert\" style=\"color:#FF8C00\"></span> <b>Response Time:</b> <font class=\"text-danger\">" . $value->exectime . " ms</font> <br>";
            }else{
                echo "<b>Response Time:</b> " . $value->exectime . " ms<br>";
            }
            echo "<b>Timestamp:</b> " . $value->timestamp . " <br>";
            echo "<b>Signature:</b> " . $value->signature . " <br>";
            echo "<b>Request Data JSON:</b> <pre>" . $value->request . "</pre> <br>";
            echo "<b>Response Data JSON:</b> <pre>" . $value->response . "</pre> <br>";

        }
        unset($value);

然而这个不起作用并抛出:注意:未定义的变量:第27行的/path/functions.php中的uri

foreach ($apiCallLog as &$value) {
    $value->print_debug_info();
}

1 个答案:

答案 0 :(得分:1)

print_debug_info中,更改

$this->$uri

$this->uri

此外,更改所有内容(在$中的->后移除print_debug_info。这是访问类参数的正确方法。这些可能只是代码中的拼写错误。 ;)