0

There are two ways to get the body of a HttpResponse.

$http = new HttpSocket();
$response = $http->get('http://www.cakephp.org');
$response->body;

and the method

$http = new HttpSocket();
$response = $http->get('http://www.cakephp.org');
$response->body();

both return the same but whats the difference between the property and the method ?

Shapi
  • 5,493
  • 4
  • 28
  • 39
oneandonlycore
  • 480
  • 6
  • 23

1 Answers1

2

I think the method was added later, but not sure. AFAIR many methods in newer 2.x versions were backported from 3 to make the API more consistent so that people who still use Cake2 can update their 2.x application to the 3.x interface to have less trouble when finally upgrading. So always use the method over the property in the case both exist.

For the technical reason see this question: Calling the variable property directly vs getter/setters - OOP Design The reason to use a method over a property is explained in the second answer, the one that has more votes. Alsoe see this one: Properties vs Methods

TL;DR:

You lose the ability to implement special get/set logic on a particular property. For properties that are scalars (strings, integers, booleans) maybe this is no problem. But what if you have a property that is a lazy-loaded class instance?

An example would be an API response that you want to parse and return something else or throw an exception if the API returned an error code. You then extend HttpSocket and overload the body() method.

But I would recommend you to read the whole answer, it is pretty good.

Community
  • 1
  • 1
floriank
  • 25,546
  • 9
  • 42
  • 66