NYCPHP Meetup

NYPHP.org

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

Michael B Allen ioplex at gmail.com
Sat Jul 19 22:46:57 EDT 2008


On Sat, Jul 19, 2008 at 9:17 PM, Jake McGraw <jmcgraw1 at gmail.com> wrote:
>
>
> On Sat, Jul 19, 2008 at 5:36 PM, Michael B Allen <ioplex at gmail.com> wrote:
>>
>> I have a class that implements __get which retrieves the named
>> attribute from a $this->_data array. In the past I believe I was able
>> to do the following:
>>
>>  $a = &$this->a;
>
> Could you post the code for your __get function?

    public function get($name, $default = null, $flags = 0)
    {
        if ($this->_data !== null && array_key_exists($name, $this->_data))
            return $this->_data[$name];
        if (func_num_args() === 1 || ($flags & self::FLAG_NO_DEFAULT) != 0) {
            require_once 'Plex/Exception.php';
            throw new Plex_Exception("No such member: $name");
        }
        return $default;
    }
    public function __get($name)
    {
        return $this->get($name);
    }

> I think there are two
> possible reasons for the discrepancy between 5.2.* and whatever version you
> were running before:
>
> 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

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



More information about the talk mailing list