Question

I'm just newbie in the Yii framework and I'm experimenting with Yii components. Let I've a simple test component:

class document extends CApplicationComponent{
        private $_width='150';
        public function init(){
            echo "This is document component init method.";
        }
        public function getWidth(){
            return $this->_width;
        }
    }

If I'm using this component in the code like the following:

Yii::import('application.components.document');
$width=Yii::app()->document->width;

then the component's init() method is invoking. Who is called this method? Can you get a refer to the line in the Yii source code on which this method is invoked.

Was it helpful?

Solution

From the API page for the IApplicationComponent interface which CApplicationComponent implements.

After the application completes configuration, it will invoke the init() method of every loaded application component.

After digging a little deeper this is the source code for CModule::setComponent(). CModule is the base class for all module and application classes therefore the _init will be called by your base application class.

elseif($component instanceof IApplicationComponent)
{
    $this->_components[$id]=$component;

    if(!$component->getIsInitialized())
        $component->init(); // <----

    return;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top