PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?
class A {
function P1() {
//do thing 1 for me
//do thing 2 for me
//do thing 3 for me
//do thing 4 for me
//do thing 5 for me
}
}
//then I do
class B extends A {
function P2() {
}
}
?>
So i added one extra function here P2(). In this class I inherted function P1() , I do not want to over write the whole
function P1(), I just want to change the very first line of code 'Do thing X for me' and rest can come from the parent class, can i do that?
class A {
function P1() {
//do thing 1 for me
//do thing 2 for me
//do thing 3 for me
//do thing 4 for me
//do thing 5 for me
}
}
//then I do
class B extends A {
function P1(){
parent::P1();
// overwrite thing 1 (redo whatever it did, but in your own way)
}
function P2() {
}
}
<?php
class A {
function doTheFirstThing() {
//do thing 1 for me
}
function doEverythingElse() {
//do thing 2 for me
//do thing 3 for me
//do thing 4 for me
//do thing 5 for me
}
function P1() {
doTheFirstThing();
doEverythingElse();
}
}
class B extends A {
function P2() {
doEverythingElse();
}
}
?>
thanks ..actually i can not change the parent class. Parent clas is already there. And it has a function where I need to make a change only in the middle. I think I will have to re-write the whole function in my extended class , that function will over write the parent function , I do'nt see any other way, right?
That's a very fragile solution though. If you are able to negotiate with the parent class, I'd recommend factoring out those discrete steps into template methods, like: