Design Question: Where To Store General Functions

What is the best place (and approach) to store in Yii application general functions, that are used across entire project?

For example, some private, custom text-transforming functions, some string encoders or for example regular-expression-based function for detecting, if user is using a mobile browser (if not using a special extension for this purpose)? Or where to keep functions that generate some prepared strings or arrays – like for example commonly used (across entire application) htmlOptions or some anonymous Javascript code attached directly to some widgets events.

I would call them all a helper functions, but I may be wrong.

So: what is the best way of keeping them and in what form should they be written – widget, behavior or just a pure PHP code closed in form of a simple class?

[size="2"]Currently I have one class, named CFR (stands for Common Function Repository), from which I call these functions for example like that: CFR::isMobileBrowser(). This class is stored directly under /protected/components.[/size]

[size="2"]But I would like to know, if this is acceptable in terms of professional coding, or if there would be a better approach.[/size]

Hi Trejder,

I am pretty sure you’ve seen this wiki but anyway:

http://www.yiiframework.com/wiki/31/

Written by the Yii inventory himself. What Qiang seems to suggest here is that you create a php file and put all your functions in it just like in the wiki. As for locations he only suggests to place the file in the protected/ folder and NOT in components that you plan to release as reusable extensions! To me (but that’s just me) this looks like your own third party code so I would go for creating a folder under extensions/ and put your function(s) in that folder and then include it in your index.php file. But there is nothing wrong with placing it in components/ folder

Hope this helps (to at least trigger a discussion) :)

I prefer to try to generalize the features that I have created over time and put them in one or more specific classes, then to create a component or an extension, to reuse possibly also in other applications.

For example, for the manipulation of date (different database), I created a class DateHelper and within a number of static methods that I can call. In this way, the resulting code is clean and reusable, traceable and also documented (if you use tools like doxygen or other).

Perhaps something like this

http://www.yiiframework.com/forum/index.php/topic/10079-enum-db-type-in-yii/page__view__findpost__p__68017

I separate the functionality into different (generally static) classes and place them in the components directory. They tend to have the name XHelper.php.

Make sure to extend CComponent.

The general idea of ZHtml sound interesting, however my current CFR contains also functions not responsible for text formatting. So the idea of splitting and grouping helpers in /protected/components/helpers also does seems interesting.

What will this give me?

(Aaarrgh! "Action failed: You have reached your quota of positive votes for the day")

The API docs explain it quite well:

http://www.yiiframework.com/doc/api/1.1/CComponent

I find the property based access to be the most useful.

I know, what is the purpose of CComponent, and I know, this is useful, as this is base for all Yii classes.

I just don’t see reasons for extending such simple class like a helper form CComponent or any other class.

No it’s not strictly necessary, it just integrates your components more closely with the Yii ecosystem. It’s nice to be able to use the same conventions throughout your code base.

I’ll amend my previous statement to:

Make sure to extend CComponent if you want to be able to use the features that Yii provides. :D

How nice! :]

Unfortunately, my dev-cheer is all you can get, because I’m out of +1 for today! :expressionless: