I have the following case with class inheritance:
class Entity
{
public $db; //connection
...
}
and classes Customer and Product extending Entity class:
class Customer extends Entity
{
public $cname;
public $cdesc;
private function populate($row)
{
foreach($row as $key=>$val) $this->$key=$val;
}
public function fetch()
{
$sql="SELECT cname,cdesc FROM customers";
$rst=$this->db->query($sql);
$row=$rst->fetch_assoc();
$this->populate($row);
}
}
class Product extends Entity
{
public $pname;
public $pdesc;
private function populate($row)
{
foreach($row as $key=>$val) $this->$key=$val;
}
public function fetch()
{
$sql="SELECT pname,pdesc FROM products";
$rst=$this->db->query($sql);
$row=$rst->fetch_assoc();
$this->populate($row);
}
}
As one can see here, each child class has the same function populate($row) which gets database row fetched from child class and populates corresponding class' variables; this function automatically fills variables: $this->cname=$row['cname'], $this->cdesc=$row['cdesc'] etc. (look at my other post here).
Now I'd like to pull this function from children to parent class Entity and inherit it by all child classes but there's a problem. This function with $this->$key=$val dynamically fills (tries to fill) parent class variables and I'd like to fill child class variables. How to define that this function fills child class variables? (I'd like to have here something like child::$key=$val but child:: does not exist).