Why Use Parent:: ?

Hi,

In a lot of examples I see things like:




parent::afterValidate();


//or

return parent::beforeSave();




In methods like:




	protected function beforeSave()

	{		

		if(!$this->hasErrors())

		{

                    // Do something.

		}

		return parent::beforeSave();

	}




I can’t figure out why you have to call parent::afterValidate(); when you already in afterValidate()?

Could someone help me out?

They may have other pieces of code that need processing in the parent class. For beforeSave(), I believe that it will not allow you to save the record/s if it does not return true.

Tnx hs2323.

I think I found more info in the 3th point of the answer of this question:

So is it correct that when I use an extended method of (for this example) the CActiveRecord class, that I always have to call the parent method first?

Usually yes, unless you want to completely block the parent’s default processing of the method, for example if you’re substituting your own code for it.

If you do block out the parent method, make sure to also return a value (usually true/false) if the parent function does.

I would suggest you always call the parent in some cases Yii uses these events/callbacks internally

Tnx a lot people. I will use return parent::[i]name/i from now on.