Let's discuss two snippets of the source code

Recently I have read the source code, and there are two questions in my mind, Let’s discuss it:

1)CApplication.php


public function getDb()

{

return $this->getComponent('db');

}

why need this? In fact, when we access Yii::app()->db, the method __get() of Class ‘CModule’, which is the parent class of CApplication, will invoke getComponent(‘db’) for us, it can’t reach the __get() method of ‘CComponent’ to invoke getXXX() method.

2)CComponent


public function raiseEvent($name, $event)

{

...

				if(is_string($handler))

					call_user_func($handler,$event);

				else if(is_callable($handler,true))

				{

					if(is_array($handler))

					{

						// an array: 0 - object, 1 - method name

						list($object,$method)=$handler;

						if(is_string($object))	// static method call

							call_user_func($handler,$event);

						else if(method_exists($object,$method))

							$object->$method($event);

						else

							throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".',

								array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>$handler[1])));

					}

					else // PHP 5.3: anonymous function

						call_user_func($handler,$event);

				}

				else

					throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".',

						array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>gettype($handler))));

...

}

Why don’t we just replace it with:


				if(is_callable($handler, false)){

					call_user_func($handler, $event);

				}else{						

					throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".',

							array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>gettype($handler))));

				}

				

Thank you for your discussing, I expect it very much.

Is there anyone here? Who can answer my questions or at least discuss with me …