How to override a method from a module model in yii?

I want to use a third party module in my multiple yii projects. For example, the module comments

Usually you need to make some changes in a module for a specific project. If I need to use costom considerations, I'll just copy them into the theme and modify. How can I override a method from a php class that is in a module? For example, I want to change a method in a model class. How can i do this?

Thank you in advance


source to share

1 answer

If you use this repo comment module a lot but it just seems like it just isn't the way you like (or need to), then unwrap it!

Write the repo and edit the code the way you like. Only you know how you need to work. Add in your own custom functions, your own helpers, wipe out existing functions entirely to work the way you want.

Remember, with OOP, your focus is reusability. Create classes and functions that can be used for future projects.

If you have a function that you use every time, but for 1 in 10 projects, you need to change it and then expand it. You can extend your class and override the function. So the only thing that would change would be to add a new class that extends your existing one and ONLY has those few features you need to override.

To expand on it, you will simply create a new file in components like someone said. If you don't customize the main configuration, all files in the component directory are automatically loaded. Just make sure the filename is the same as the class name!

Take a look at your comment Module, for example. They extend CWebModule. CommentsModule has all the functions and variables that the CWebModule has, but can have more functions / variables or override those defined in the CWebModule. He essentially captured him.

Class CommentsModule Extends CWebModule{}




All Articles