Thursday, January 10, 2013

Preserving the Functionality of The Parent [Part 1]

Sometimes you want to preserve the functionality provided by the parent .You don't need to completely override the function;you just need to add something to it . You could copy all the code from the parent method into the subclass's method ,but as you have already seen,OOP offers you better ways of doing this than just copying lines of code.

To call the functionality provided by the parent,use the syntax parent::[function name] . when you just want to add additional behavior to a method,first you call parent::[function name]  and then add your additional code. when extending a function in this way,always call the method on the parent before doing anything else.Doing so ensures that any changes to the operation of the parent won't break your code.

    because the parent class may be expecting the object to be in a certain state,or may alter the state of object ,overwrite property values,or manipulate the object 's internal data,always invoke the parent method before adding your own code when extending an inherited method.


No comments:

Post a Comment