Authoring Yii Extensions

So, in your opinions what are some of the best practices and guidelines for writing good Yii extensions or any other library? I’ve been quite into Yii lately and really feel like contributing to the community. I’m not a very experienced programmer, but have been teaching myself for about a year or so. Some of my initial questions are:

  • Should I use Yii:trace() and Yii:t()?

  • Should I throw exceptions? Only CException and subclasses?

  • Does Yii have any programing style I should observe? e.g. Array configurations in constructors.

  • Quite much everything else on how to build robust, scalable and user friendly code.

I do know about documenting and unit testing so you can leave that out.

Please do enlighten me and maybe others with similar questions. Thanks!

I could use some reading of great examples, short ones hopefully.

Depends on extension.

Only if you need to.

You can check Yii source code but generally we aren’t forcing to use any specific style.

Could you give some examples as to when to use Yii:trace() and Yii::t()?

Is it best to not throw exceptions and return false instead? Because in Java it’s almost about throwing exceptions.


Use Yii:.trace() when you think, something interesting is happening in your extension which could help others when they debug their code. These messages are only logged if YII_DEBUG is true. I often use the following construct, to avoid calls to Yii::trace() in the first place, if not in debug mode:

YII_DEBUG && Yii::trace(some_expensive_formatting_call(),'application.component.mycomponent');

Use Yii::t() if your extenstion contains text that is visible to the end user and you see a chance that someone might need that text in other languages.

Throw exceptions only in fatal error conditions, like when your code can not continue to execute because a dependency failed.

I see where you’re getting at. Thank you!