NYCPHP Meetup

NYPHP.org

[nycphp-talk] Change in magic method + reference behavior?

Michael B Allen ioplex at gmail.com
Sat Jul 19 23:55:05 EDT 2008


On Sat, Jul 19, 2008 at 10:55 PM, Jake McGraw <jmcgraw1 at gmail.com> wrote:
>
>> > 1. You're using "Call-time pass-by-reference" by marking your return
>> > value
>> > with an ampersand, try changing your code to this:
>> >
>> > public function __get($field) {
>> >   return &$this->_data[$field];
>> > }
>>
>> But I don't want the default behavior to be to return a reference.
>>
>> Mike
>
> Ah, well, you're setting yourself up for some fun debugging later on, it's
> probably best not to mix and match pass by reference/value, which is why I
> think the PHP group depreciated the ability to do call-time pass by
> reference. IMO, whether a function returns a reference or a value should be
> part of the object method definition, not on the basis of the way the
> function was called.

I did not use, do not use and do not want to use call-time pass-by-reference.

The code in question was "&$this->a" where $this is "magic" so it
should, in theory, behave as though "a" was a real object member. If
that code did in fact work like I thought it did, it must have been
doing some extra "magic" because my __get function does not specify a
reference in the signature or in the return.

I suppose someone could have decided this case was call-time
pass-by-reference in practice since it does go through the magic
functions, declared it a bug and changed it.

But I'm fine with &$this->_data['a']. I was just curious to know if
something fundamental had changed. At this point I'm not sure if the
code ever worked the way I claim and I don't care enough to write test
programs to find out.

Thanks tho,
Mike

-- 
Michael B Allen
PHP Active Directory SPNEGO SSO
http://www.ioplex.com/



More information about the talk mailing list