Stumped

I can’t quite understand this.

In this day and age where pass by reference is the new black (ie, .net only passes by reference, java only passes base types by value), php has decided to be the black sheep (or not, depending on your point of view).

Using php 5, objects are passed by reference, always, which is logical, easier to manage and saves on stack space. OTOH arrays are passed by … value (unless forced with a &). Go figure.

Still trying to come to terms with this particular oddity.

Powered by ScribeFire.

3 Responses to “Stumped”

  1. philou Says:

    Hello David,

    Well, it may be a vestige kept from the php4 era, in a ancient world where even objects were passed by value. This php4 idiom makes everything more painful, because you would seldom use any ‘=’ to pass an object by value if you want to do something reasonable with it. Unfortunately, it’s easy to forget typing ‘=&’ instead of ‘=’. Honestly, I can’t see any benefit in this.

  2. d.mills Says:

    Actually, there is some sort of logic in it, in that ‘base types’ are passed by value, as opposed to object types.

    That said, it still rings false to anyone who doesn’t come from a VM background, since in ‘normal’ (ie without a garbage collector) programming, the difference is between pointers and types, and arrays and objects are more or less systematically pointers to allocated memory.

    Still, I can’t imagine the amount of time wasted passing arrays on the stack in php thanks to this!

Leave a Reply