Ok so this is just to ask from the guru’s out there when, how and why you’d use a component. For example looking at the default components made, I understand that main menu is a widget setup for displaying the main menu. But then you have the user identity which is used to control user access with login authentication.
Reading the guide, you get the hint that a component is an object to handle specific public variables and events. As to what that implies exactly I’m a little confused.
I think from what I understand, component is like free for all in which you can define different things for your site specifically. Example, lets say you wanted to create your own rule sets (for password strength, capitalizing the first letter on names, valid postal code / zip code w/e). You would create these "functions" within a component and then use that component as a reference. Am I close?
Any shade of light on the subject would be great .
I don’t understand this approach. If you have a class that has getters and setters, then why not subclass it from CComponent? Job done for free; no code written. And, if you later decide to use your class as a Widget or anything else down the Yii object chain, then your class is already Yii’ified.
Also, static methods (that take a parameter) can be a symptom of the feature envy code smell. It’s an interesting exercise to take some legacy code (of a non-trivial application) and remove its static methods – where they aren’t really static – and watch the cascade of simplification that occurs.
Imo, There is no correlation between statics and improved reusability; I’d argue the opposite.
I’d definitely argue that performance is worse with overuse of statics. But then I regard performance issues as something entirely separate and a post development issue, and I’d rather concentrate on good quality maintainable code than an early focus on performance when it might never be needed.
First off, I’m not wilfully disagreeing with you, pestaa. I see how you help the Yii community and I have a lot of time for you because of that. We’re on the same team.
To answer your question, it depends, but that’s why I qualified my answer; I know I’m talking to informed people.
I know what a static method is and what it entails. I use static methods as an interview question. I even tell interviewees that I will be asking them about static methods. It’s not going to help anyone who hasn’t been using them for years
There are plenty of cases for static methods. Any Singleton getInstance(), for example. I’m not “against” statics, they have their place, but they mustn’t be overused.
It all depends. But until there is a performance issue, I genuinely don’t care.
I step through Qiang’s code routinely. I don’t agree with all his choices. He’s good, but I’ve worked with better.
In any system I refactor, the first thing I do is remove globals.
No offence taken, and healthy debate is always good; it’s great to learn. Glad to hear you’re not a static-junky – I didn’t think so. It actually got me thinking about Yii’s AR model() function, which was at the back of mind to examine. But it seems its just a way of avoiding multiple static methods by instantiating its own object just for AR purposes, which is cool. See my PM for OT stuff.
It might look bad idea to define find methods as non-static in ar models at first sight, since they have nothing to do with the current database row, but if you think about chainable, short and easy-to-maintain selection, you thank model() for its existence.
[quote name=‘qiang’]static classes […] are more difficult to be extended and customized.
It depends I think. It is easier for me to extend a static class with many protected properties than it is to customize non-static class with no or private attributes.