Class that runs a query 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-&gt;_var1= $this-&gt;$var1;
        $this-&gt;_var2= $this-&gt;var2;
       $this-&gt;_var3= 8;  // i need this 8 if no value is passed

    }

    public function testMethod(){
       $query = "SELECT * from table where id = ".this-&gt;_var1." AND name= ".$this-&gt;var2." AND x= ".$this-&gt;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-&gt;testMethod();


How can I improve this?  Or is this even a good way to do it?
I'm not sure what you want to achieve with the constructor.

public function __construct($var1,$var2, $var3)
    {
        $this-&gt;_var1= $this-&gt;$var1;
        $this-&gt;_var2= $this-&gt;var2;
        $this-&gt;_var3= 8;  // i need this 8 if no value is passed

    }


If you want to set the properties to the given parameters, you should do

 $this-&gt;_var1= $var1;




And with

$this-&gt;_var3= 8;  // i need this 8 if no value is passed


you always set property $_var3 to 8, nonetheless if $var3 is being passed.

For setting a default value to a paramter you can write it like this

public function __construct($var1, $var2, $var3 = 8)
        {
            $this-&gt;_var1 = $var1;
            $this-&gt;_var2 = $var2;
            $this-&gt;_var3 = $var3;

        }


so if you call

$r = new testClass($var1, $var2, 10);


$this->_var3 would be set to 10, and if you call

$r = new testClass($var1, $var2);


$this->_var3 would be set to the default 8.



In your testMethod you're using $this->var2 &amp; $this->var3, without the underline. These properties are not defined.



return $runQuery($query);


remove the $ in the function's name, or it would be taken as variable.



In your SQL-Query there's a column named "name". In all cases where the column types are strings, you have to write the value in single quotes.

$query = "SELECT * from table where id = ".this-&gt;_var1." AND name= '".$this-&gt;_var2."' AND x= ".$this-&gt;_var3; 




For debugging purposes, set

ini_set('display_errors', 1);
error_reporting(E_ALL);


to the starting script.