I want to extend some built-in framework classes, but I there are some points I ‘m not clear about:
- I assume I should put my classes under my components folder, is this the right place?
- Should I name the classes like
Subfolder_ClassNameand the files likeClassName.php? - How do I inherit? The default include paths does not include the framework itself. I assume there is a built in way to do it without me changing the include path/start playing with the autoload function or Simply hard code an include in my code.
Right?
Placement of source files
Putting them under
/protected/componentsis a natural choice. If you later develop more generic classes that can be reused across projects, you can think about putting those into a separate directory.Naming and directory structure
This will depend on the scale of your app. If you don’t have lots of components (say under 20) then IMHO you don’t need any directory structure, the
componentsdirectory is good for all of them. Another practical approach is to put your business components into one directory and your HTML components into another (e.g.widgets).Including parent classes
Your configuration file should have a section like this:
This section actually results in
CModule::setImportbeing called on your application instance to register classes that can be autoloaded. If you want to extend these classes then you don’t need to do anything.If some of your components depend on Yii classes that don’t get imported as above, you will need to explicitly import them using
Yiibase::importbefore declaring your class. For example:Registering aliases for paths
If you want to create your own aliases for import paths (e.g. to have a path
myapp.componentsyou can reference when importing classes), you can do this usingYiibase::setPathOfAlias. A good place for this is the very top of your configuration file: