I am tossing up two ways of using variables in a method within a class. Some methods have up to 20 variables used. Which example is satisfy OOP progamming structures the best and why
**Example 1:**
$sampleclass->variable1 = 100;
$sampleclass->variable2 = 200;
$sampleclass->variable3 = 300;
$row = $sampleclass->Method();
--------------------------
class sampleclass {
public function Method(){
if ($this->variable1) {
// do something
}
if ($this->variable2) {
// do something
}
if ($this->variable3) {
// do something
}
}
}
============================================
**Example 2:**
$row = $sampleclass->Method(100,200.300);
class sampleclass {
public function Method($variable1, $variable2, $variable3){
}
}
TL;DR : neither.
If you have 3 different executions based on 3 different conditions, then should have separate method for each of them. The resulting API should be something like something like:
The object are supposed to hold state. You have 3 different methods, which alter that state. And one method which returns data. No
ifstatements required.To learn a bit more about the subject, I would recommend you to watch this and this lecture.