yiisoft / yii

Yii PHP Framework 1.1.x
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
4.85k stars 2.28k forks source link

CMap and return by reference #1573

Closed nizsheanez closed 11 years ago

nizsheanez commented 11 years ago

1) Why CMap have 'readonly' property but it's not using now? 2) Why CMap::itemAt don't return by reference? If i want use CConfiguration for example and send it to CMenu, than i have exception "Indirect modification of overloaded element of СConfiguration has no effect" on string https://github.com/yiisoft/yii/blob/master/framework/zii/widgets/CMenu.php#L270

That happen when we try to edit CMap on level 2 or deeper, like this - $items[$i]['label'] = 'some_label'

But, I don't know how we can implement 'readonly' feachure if CMap::itemAt will return by reference.

creocoder commented 11 years ago

1) Why CMap have 'readonly' property but it's not using now?

It can be used in children classes.

2) Why CMap::itemAt don't return by reference?

Push values by reference and it will return references.

That happen when we try to edit CMap on level 2 or deeper, like this - $items[$i]['label'] = 'some_label'

If shortly, PHP happen :-) More about "Indirect modifications of overloaded elements" and why it happens you can find easy.

samdark commented 11 years ago

1) Actually it's used. Check code again.

nizsheanez commented 11 years ago

1) Yep, sorry 2)

Push values by reference and it will return references.

Need to try it tooday, And need to try sudh values by reference and set readonly=>true

nizsheanez commented 11 years ago

So, i try it. Push by reference i can't 1 - return parent::__construct(&$data); => Call-time pass-by-reference has been removed 2 - Owerride interface of __construct or add methods too cant, they must compatibile with CMap,

3 - RecursiveArrayAccess like this:

__construct($data)
{
        foreach ($data as $key => $val)
        {
            if (is_array($val))
            {
                $this->add($key, new self($val, false));
            }
            else
            {
                $this->add($key, $val);
            }
        }
}

too not working with: array_values https://github.com/yiisoft/yii/blob/master/framework/zii/widgets/CMenu.php#L295

4 - Implement Iterator and IteratorAgregate same time i too can't

creocoder commented 11 years ago

May be then its good idea to make own class for own needs?

nizsheanez commented 11 years ago

May be. I even have a similar class at work. But using CMenu or CForm with CConfiguration also not bad idea :-)

cebe commented 11 years ago

you do not need to add & when data is an object. objects are given by reference in php by default. I can't see a problem with data as reference here.

That happen when we try to edit CMap on level 2 or deeper, like this - $items[$i]['label'] = 'some_label'

this is not possible with php array interface.