Page 1 of 1
OOP related question
Posted: Thu Oct 05, 2006 11:57 am
by iffo
Please use PHP and CODE tags with posting code.
Hi,
suppose I have a class
Code: Select all
<?
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?
Thanks
Posted: Thu Oct 05, 2006 12:08 pm
by Luke
Is this PHP5? This is the best answer I can give you without seeing more code, or more elaboration.
Code: Select all
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() {
}
}
Posted: Thu Oct 05, 2006 12:08 pm
by volka
e.g.
Code: Select all
<?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();
}
}
?>
Posted: Thu Oct 05, 2006 1:36 pm
by iffo
Thanks a lot. What about the code I want to change is some where in the midle of the function
suppose if the function is in the parent class is
function P1(){
do thing 1
do thing 2
do thing 3
do thing 4
}
and I want to change only 'do thing 3' to do thing 100' how will I do that?, rest can come from the parent function
Posted: Thu Oct 05, 2006 1:39 pm
by volka
Then split it up in three parts.
Or use a parameter to control the method's behaviour
Code: Select all
function P1($xyz=true) {
//do thing 1 for me
//do thing 2 for me
if ($xyz) {
//do thing 3 for me
}
//do thing 4 for me
//do thing 5 for me
}
Posted: Thu Oct 05, 2006 6:13 pm
by iffo
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?
Posted: Thu Oct 05, 2006 6:32 pm
by Luke
just copy the method from the parent class and make ajustments as you see fit...
Posted: Thu Oct 05, 2006 6:44 pm
by Ambush Commander
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:
Code: Select all
function P1($xyz=true) {
$this->doThing1($xyz);
$this->doThing2($xyz);
$this->doThing3($xyz);
//...
}
and then only redefine doThing3
Posted: Thu Oct 05, 2006 10:20 pm
by iffo
No I do not have luxury to change the parent class, so i think I will have to just copy the whole function in my extended class and make modifiactions
Posted: Fri Oct 06, 2006 3:17 am
by Jenk
Why not?!
It's a sure fire sign that it needs rework when you need to work on it..