1

I've been handed a PHP class, and I'm not interested in fully restructuring it. (it works!)

But I'd like to add a slight modification inside a few methods.

Here is one of the many methods inside the class:

<?php
class SomeFunClass {

    public function getAccountInfo()
    {
        $request  = $this->prepareRequest('get_account_info');
        $response = $this->execute($request);
        return $response;
    }

}
?>

The return $response is a string value. I've come to a point that I need to return the $request string, which happens to be a json string.

The prepareRequest() method always returns a json string, which is then passed to the exec() method, which simply sends the data via cURL to a domain.

I'd like to extract the $request string (when I call the getAccountInfo() method), for later review.

Here's what I'm doing now:

<?php

   $api = new SomeFunClass();
   $curlresponse = $api->getAccountInfo();

?>

Obviously, the example immediately above only gives me back what the cURL response would be. Would be nice to call a method that lets me see what the $request looks like. I'm open to suggestions.

1
  • You won't be able to get at the $request variable unless you modify the source code of SomeFunClass::getAccountInfo. There's no magic way to reach into the method and get a variable out. Well, unless you're talking about a live debugger. Commented Nov 21, 2013 at 20:15

4 Answers 4

2

Just return an array with the request and the response:

<?php
class SomeFunClass {

    public function getAccountInfo()
    {
        $request  = $this->prepareRequest('get_account_info');
        $response = $this->execute($request);
        return array('request' => $request, 'response' => $response);
    }

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

Comments

1

You can modify those methods to store the last request into an attribute of the current class :

<?php

class SomeFunClass {

    $last_request;

    ...

    public function getAccountInfo()
    {
        $request  = $this->prepareRequest('get_account_info');
        $last_request = request;
        $response = $this->execute($request);
        return $response;
    }

    public function getLastRequest()
    {
        return $this -> last_request;
    }
}

?>

Or, better, if prepareRequest is a method of yours, then just modify this one to store the last request.

Comments

0

You can do something like this:

<?php
class SomeFunClass {

    public $request;
    public $response;

    public function getAccountInfo()
    {
        $this->request  = $this->prepareRequest('get_account_info');
        $this->response = $this->execute($this->request);
        return $this->response;
    }

}
?>

Now, you can do something like this:

<?php

   $api = new SomeFunClass();
   $curlresponse = $api->getAccountInfo();
   $request = $api->request;
?>

Ideally, you can do implement your class like this to take actual advantage of OOP (so that these instance variables request and response are auto-set for all your methods):

<?php
class SomeFunClass {

    public $request;
    public $response;

    public function getAccountInfo()
    {
        $this->prepareRequest('get_account_info');
        return $this->execute();
    }


    public function anotherMethod()
    {
        $this->prepareRequest('another_method', 'some', 'args');
        return $this->execute();
    }

    public function prepareRequest()
    {
        $args = func_get_args();      // contains your arguments
        $method = array_shift($args); // contains your method name
        ...
        ...
        $this->request  = $return // value returned by this method
    }


    public function execute()
    {
        $request = $this->request;
        ...
        ...
        $this->response = $return // value returned by this method
    }
}
?>

3 Comments

Why would you want to save unnecessary information to the object?
Cause it makes less modifications than modifying the return of those methods, which implies to modify scripts that use it too.
@qwertynl: added example code on why someone would prefer my kind of approach :)
0

You could also do this:

<?php

class SomeFunClass {

    public function reviewRequest($request)
    {
        return $this->prepareRequest($request);
    }

}

And then:

<?php

   $api = new SomeFunClass();
   $request = $api->reviewRequest('get_account_info');

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.