-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #16247: clone behaviors when cloning components #16430
Conversation
brandonkelly
commented
Jun 22, 2018
Q | A |
---|---|
Is bugfix? | yes |
New feature? | no |
Breaks BC? | yes |
Tests pass? | yes |
Fixed issues | #16247 |
Merged. Thanks! |
Why events are not cloned in the same way? |
Should be cloned as well, I think. Want to make a pull request? |
Events don’t seem as obvious as behaviors. Cloning an object is basically a shortcut to defining a new object with the same class and setting the same properties on it - and if behaviors are ment to act as extensions of the class, that definitely seems like it should be cloned too. But events are intentionally registered either at the class level (already will work on a clone) or for a specific instance. Maybe it's not intended that the events should be set on the clone as well though. |
Hmm... right. That's controversial. |
I don't see any difference. It looks inconsistent to me, especially that events attached from behaviors are cloned, but events attached manually are not. But AFAIK there will be some changes in events handling in 3.0 so it may be irrelevant after all. |
Events added from behaviors are essentially the class dynamically defining its own business logic. The non-behavior equivelant would have been for the owner class to just call some internal method alongside firing the event. I’m not saying it definitely doesn’t make sense for events to be cloned as well; just that it’s not always going to be expected behavior. For example, if I did this: $obj = new MyObject();
$obj->on('afterDoSomething', function() {
// ...
});
$obj2 = clone $obj;
$obj2->doSomething(); I don’t think expected behavior would be that the |
@brandonkelly Let's just agree to disagree. For me current behavior is inconsistent and I don't see any reason why dynamically attached behaviors and its events should be handled differently than dynamically attached events. But this is not important for me, I almost never use this feature anyway. I just wanted to point it now, because fixing this inconsistency may require BC break (you cannot just clone all events because cloned behaviors already attaches its events, so you will get duplicated events). |
Let's not clone events for now. |
This fixes a 3.0 bug introduced in yiisoft#16430. If a component's behaviors() method returns any behaviors without a name, then cloning it would result in the behavior getting attached twice.
This fixes a 3.0 bug introduced in #16430. If a component's behaviors() method returns any behaviors without a name, then cloning it would result in the behavior getting attached twice.
This fixes a 3.0 bug introduced in yiisoft/yii2#16430. If a component's behaviors() method returns any behaviors without a name, then cloning it would result in the behavior getting attached twice.
This fixes a 3.0 bug introduced in yiisoft/yii2#16430. If a component's behaviors() method returns any behaviors without a name, then cloning it would result in the behavior getting attached twice.