OOP related question

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!

Moderator: General Moderators

Post Reply
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

OOP related question

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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() {

}
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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();
	}
}
?>
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
}
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

just copy the method from the parent class and make ajustments as you see fit...
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Why not?! :?:

It's a sure fire sign that it needs rework when you need to work on it..
Post Reply