5

I am working on wordpress research recently and trying to dig how the query mechanism is handled.
I came across a function definition like this
function &get_post(&$post, $output = OBJECT, $filter = 'raw')
I have not seen anything similar before and not sure if the ampersand(&) has to do something with the address.(Thinking traditionally the use of ampersand(&) ).
I am naive in this and spare me if its something easy.
Can anyone give their thoughts on this.It would help me in moving forward.Tried to find it through google and documentation but did not get find a solution.
Thanks

coderunner
  • 925
  • 1
  • 18
  • 33
KillABug
  • 1,414
  • 6
  • 34
  • 69

1 Answers1

4

The first & on the function declaration causes PHP to return a reference. The & on $post makes changes to the actual variable that you pass into the function.

It's actually unclear to me why either of these (the reference declarations) are used. It seems to me to be an incorrect attempt at optimization on Wordpress' part, or it is more likely for compatibility with PHP 4. I think you can just ignore the ampersands for your purposes.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • Thanks for the explanation.It means the function holds a reference as a parameter in context of **&$post**? – KillABug Nov 23 '12 at 05:27
  • 1
    [Here's](http://stackoverflow.com/questions/1676897/what-does-it-mean-to-start-a-php-function-with-an-ampersand#1676963) one more topic about that - basically like @Explosion Pills said, it's most-likely that they did that in order to improve performance in PHP4, since in PHP 4 objects were copied every time when they were passed--around(which with big objects, or big queries could compromise productivity I guess). – Nikola Ivanov Nikolov Nov 23 '12 at 08:21
  • @sachlearner it would mean that `$post` could be modified, but I looked at the function and it is not modified so if you are not using PHP 4 that is pretty much meaningless – Explosion Pills Nov 23 '12 at 19:09
  • @ExplosionPills Accepted the answer,and thanks for the help.you have an interesting name though. ;-) – KillABug Nov 24 '12 at 01:26