Skip to content
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

Merged
merged 1 commit into from Jun 22, 2018

Conversation

brandonkelly
Copy link
Contributor

Q A
Is bugfix? yes
New feature? no
Breaks BC? yes
Tests pass? yes
Fixed issues #16247

@samdark samdark changed the base branch from 2.1 to 3.0 June 22, 2018 18:18
@samdark samdark merged commit e1a49cf into yiisoft:3.0 Jun 22, 2018
@samdark
Copy link
Member

samdark commented Jun 22, 2018

Merged. Thanks!

@rob006 rob006 added this to the 3.0.0 milestone Jul 7, 2018
@rob006
Copy link
Contributor

rob006 commented Jul 7, 2018

Why events are not cloned in the same way?

@samdark
Copy link
Member

samdark commented Jul 7, 2018

Should be cloned as well, I think. Want to make a pull request?

@brandonkelly
Copy link
Contributor Author

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.

@samdark
Copy link
Member

samdark commented Jul 7, 2018

Hmm... right. That's controversial.

@rob006
Copy link
Contributor

rob006 commented Jul 7, 2018

Events don’t seem as obvious as behaviors.

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.

@brandonkelly
Copy link
Contributor Author

especially that events attached from behaviors are cloned

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 afterDoSomething handler would get called for $obj2. If you disagree, maybe it’s worth opening this up as a new issue for debate there. This PR is probably the wrong place to discuss it :)

@rob006
Copy link
Contributor

rob006 commented Jul 8, 2018

@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).

@samdark
Copy link
Member

samdark commented Jul 8, 2018

Let's not clone events for now.

brandonkelly added a commit to brandonkelly/yii2 that referenced this pull request Aug 23, 2018
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.
samdark pushed a commit that referenced this pull request Aug 27, 2018
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.
brandonkelly added a commit to brandonkelly/yii-core that referenced this pull request Aug 28, 2018
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.
hiqsol pushed a commit to yiisoft/yii-core that referenced this pull request Aug 28, 2018
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.
@brandonkelly brandonkelly deleted the 16247-fix branch February 16, 2021 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants