2

Trying to access object parameter value from the class in function print_debug_info() and getting runtime error.

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>";
    }   
}

When accessing from outside the class there is no problem to get the parameter values of the object and print them so the below works fine.

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

However this one is not working and throwing: Notice: Undefined variable: uri in /path/functions.php on line 27

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

1 Answer 1

1

In print_debug_info, change

$this->$uri

to

$this->uri

Also, change all like this (remove the $ after the -> everywhere in print_debug_info). This is the proper way to access class parameters. These were probably just typos in your code. ;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.