I have a class like this:
class someClass { public static function getBy($method,$value) { // returns collection of objects of this class based on search criteria $return_array = array(); $sql = // get some data "WHERE `$method` = '$value' $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) { $new_obj = new $this($a,$b); $return_array[] = $new_obj; } return $return_array; } }
My question is: can I use $ this the way I am above?
Instead:
$new_obj = new $this($a,$b);
I could write:
$new_obj = new someClass($a,$b);
But then when I extend the class, I will have to override the method. If the first option works, I wonβt have to.
UPDATE on solutions:
Both of them work in the base class:
1).
$new_obj = new static($a,$b);
2.)
$this_class = get_class(); $new_obj = new $this_class($a,$b);
I haven't tried them in a child class yet, but I think # 2 won't work there.
Also, this does not work:
$new_obj = new get_class()($a,$b);
The result is a parsing error: Unexpected '(' This should be done in two steps, as in 2.) above, or even better, as in 1.).