Attachable CWebUser

My point with this post is to ask for a way for modules/components to add functionality to the CWebUser class without extending it.

For example a common issue is to integrate the yii-user module and the rights module, each one as a class that extends CWebUser, to solve the problem I have to a create a new class that is a merge of both.

If each module attached their functions to the normal CWebUser I didn’t have to worry about this kind of conflicts and maybe I could use it like:




Yii::app()->user->rights->[call]



We already have behaviors for this. So the right solution would be, if those extensions provide a behavior that you can attach to your custom WebUser class.

Mike, I believe that the OP has a point. AFAIK, behaviors can’t override the methods of the class and it is sometimes required to achieve a desired effect.

So this means, we need a different mechanism, right? Because you want something that you can attach to an object (like a behavior) and which can then even override the original objects methods. I don’t think that’s possible.

Yes, this is the feature requested by the OP.

I agree that there’s no simple solution for this, if any at all.

Actually what he requested can already be done through behaviors. If you attach a behavior ‘rights’ to your WebUser object which provides a getIsGuest() method, you can do Yii::app()->user->asa(‘rights’)->getIsGuest();

Indeed, you’re right.

But not all features can be implemented as behaviors. When it is needed to override a method, because it is called by the framework, is one of such cases.

As a side note, I don’t have a problem with that.

I’m just considering OP’s case.

Ok, but that’s a different problem. You can not override any method of an application component unless you subclass it. So lightglitchs proposal is not a solution to this.

Sure.

What’s so bad about extending and overriding?

I don’t get it.

That’s OO in a nutshell.

For me it’s not bad extending and overriding, my problem is trying to make several modules/plugins to work together without me having to change their code.

Right now this is not possible because their both extend and override the same class and are not compatible.

I’d consider contacting the extension/modules author and discuss how things implemented in there could be optimized to make including them easier. For example extended classes could be changed to behaviors to be able to inject multiple things into one class.

This depends on what is done there, so if you give concrete examples we could think about how to improve that extensions.