php function edit without code duplication

johnzakarias

New member
Hi

now i have a lot of functions that exists in a file (commonfunctions.php)

and my manager want to change a specific function to do a job in a section and another job in another section by adding a condition in query 

and he is not accepting code duplication, so when i took the same function and changed it's name then adding the condition, he refused this in code review by telling me i don't want code duplication



can any one guide me how i can do this?
 
move these function to the classes, if you want to change one function (method in the class) just create a new class and extends the parent class with overriding one method that you want to change

Code:
class ParentClass
{
	public function getRandomChar()
	{
		return chr($this->getRandomNumber(32, 126));
	}
	public function getRandomNumber($min, $max)
	{
		return rand($min, $max);
	}
}

class ChildClass extends ParentClass
{
	public function getRandomNumber($min, $max)
	{
		return rand($min * 100, $max * 100) / 100;
	}
}
$parent = new ParentClass();
$child = new ChildClass();

// the same logic:
$char1 = $parent->getRandomChar(); // A, f, ~
$char2 = $child->getRandomChar(); // C, e, =


// different logic:
$number1= $parent->getRandomNumber(1, 100); // 4, 8, 29
$number2= $child->getRandomNumber(1, 100); // 1, 3.14, 67.38
 
Back
Top