[size=3]NOTE: I’ve modified the below code a bit. If you’re interested in checking this out, please look at issue #264 under development and all source code is attached to that issue.[/size]
I've started to use behaviors as a way around PHP's lack of multiple inheritance. This has worked well except for the fact that only behavior methods are accessible via the owner class.
I have been playing around with a few ways to implement the behavior properties and have settled on these changes to the CComponent class:
I considered placing them in my subclasses of CComponent but I really think they should be in the base component class if behaviors are to be fully supported.
One concern/issue/caveat/problem with this approach is that behaviors must be attached in the constructor because of the application flow. The init() method isn’t called until after the properties are set in a component. But unless there is some architectural reason this is bad practice, I don’t have a problem with it.
I'm sure I will tweak CComponent a little more before I complete my project, however I wanted to put this out there for comments.
Thanks Qiang, appreciate the feedback. I will post this as an issue.
I've been running the code for a few days with no ill effects to my knowledge.
I've been thinking some more about this implementation.
The fact that I have to attach behaviors in the constructor is bothersome to me.
The solution I've come up with is to create a "preInit()" event which can be implemented by the subclass if this behavior is required. The preInit() could contain the behavior attachments.
In addition, I've considered adding a framework class override feature to the autoload mechanism. This would allow you to "inject" your own class into the base framework without having to modify framework code. For example, I could create a CComponent class that lives outside of the framework installation but through a special import mechanism would be loaded when CComponent was requested. Again, just a thought.