how to use a function from a component class ?

hi, i have two controllers, controllerA and controllerB,

controllerA has an existing function that I also need within controllerB

what i was thinking is to create another class and put the common function there and extend it so that

both controllers can use it…

so I created a class inside the components directory and pasted the common function…

how to use that in both controllerA and B now?

Before you call the function:


how am i gonna call the function? do i need to do this



classname::funcname if static ?

All the import is doing is making the class available to your code. If the method is static then use


If it’s not, use

$obj = new ClassName;


If the method is used in multiple controllers and is specific to the controllers, you could add the method to your Controller class instead of making a new component.

In hindsight, I believe the components directory is already automatically imported by Yii, so the import call is likely redundant.

I would do this since controller A and B are going to use the same method.