2

I do not know how to ask this question, but I came to something I can't grasp in guzzle/guzzle package for Laravel. Let me show code and then I'll ask my question

$client = new \GuzzleHttp\Client();
$request = $client->createRequest('GET', 'http://foo.com');

$query = $request->getQuery();
$query['foo'] = 'bar';

$response = $client->send($request);

here foo is set in the request. How? I could understand it if $request->getQuery() would return a pointer, but that's not what really happened here isn't it.

I would expect some method to put $query object back in the $request.

2
  • \GuzzleHttp\Client => GuzzleHttp\Client ? Commented May 6, 2015 at 14:31
  • It's passed with $query['foo']. [] is adding another array to $query. ^_^ Commented May 6, 2015 at 14:31

1 Answer 1

6

Objects are passed by reference in PHP.

The $query object is implementing ArrayAccess so $query['foo'] = 'bar'; is the same as doing $query->setFoo('bar');

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

5 Comments

Part with ArrayAccess was clear to me, just I forgot that "small" part that Objects are passed by reference. Thank you, it basically means it is a pointer, so all my further questions are cleared :)
And $query itself is a reference to part of the $request object, so that modifying $query modifies $request.
It's not really a pointer like in C, php.net/manual/en/language.oop5.references.php
Pointer was easiest term I could think of to ask question. Found this in the comments on php.net When using the "->", you will of course be accessing the same internals as the original variable in the caller function. Just using "=" will only play with copies."
@Dexa usually when referring to a pointer, we want to express that it is explicit; it can be modified much like an integer variable and may support arithmetic. And we can read the information that points to some data. References OTOH are implicit. You don't directly read & write the value that points to data, but you can assign it, maybe compare it. They may or may not be reassignable, depending on the language. They are internally managed like pointers but you access the data being pointed to as if you were acting on the data itself. See also Google pointer reference.

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.