I have made this class that runs a query. I want to optimize my code and want to have some expert advice.
Here is the class:
class testClass extends Model
{
protected $_var1;
protected $_var2;
protected $_var3;
public function __construct($var1,$var2, $var3)
{
$this->_var1= $this->$var1;
$this->_var2= $this->var2;
$this->_var3= 8; // i need this 8 if no value is passed
}
public function testMethod(){
$query = "SELECT * from table where id = ".this->_var1." AND name= ".$this->var2." AND x= ".$this->var3;
return $runQuery($query);
// $runquery is another method…not in concern here..it returns the results…just take my words :)
}
}
Usage of class:
$r= new testClass($var1, $var2, $var3);
$result = $r->testMethod();
How can I improve this? Or is this even a good way to do it?
$var1, $var2, $var3
doesn't tell us enough. Second, your style is inconsistent. The constructor's opening brace is on a new line while thetestMethod()
's is on the same line. Your indentation is also pretty inconsistent. Finally, a single line of code should never be so long as to cause illegibility. You appear to know how to concatenate strings, just concatenate to$query
a few times. \$\endgroup\$