I don't know if this question has been asked before, but i have been looking for it and i couldnt see anything.
My problem is as follow:
I have two classes. The father class (Products.php) contains the following code:
class Producto {
protected $id;
protected $producto;
protected $descripcion;
//CONSTRUCT FUNCTION
function __construct($id, $prod, $description){
$this->id=$id;
$this->producto=$prod;
$this->descripcion=$description;
}
//THE NEXT TO METHODS LOOK FOR IN THE DATABASE AND UPDATE THE PROPERTIES VARIABLES
public static function db_select_producto_by_id($mysqli,$id){
$query="SELECT * FROM tb_productos WHERE id='$id' LIMIT 1";
return Producto::db_select($mysqli,$query);
}
public static function db_select_producto_by_name($mysqli,$name){
$query="SELECT * FROM tb_productos WHERE producto='$name' LIMIT 1";
return Producto::db_select($mysqli,$query);
}
protected static function db_select($mysqli, $query)
{
$result = $mysqli->query($query);
if($result->num_rows > 0)
{
$row=$result->fetch_array(MYSQLI_ASSOC);
return new Producto($row['id'], $row['producto'], $row['descripcion']);
}else
return new Producto(0, 'none', 'none');
}
The child class (Oferta.php) extends from Producto.php and contains the following code:
class Oferta extends Producto{
protected $idOferta;
protected $tipoOferta;
protected $precioOferta;
protected $descripcionOferta;
function _construct($idOferta, $tipoOferta, $precioOferta, $descripcionOferta, $id,$prod,$descripcion){
$this->idOferta=$idOferta;
$this->tipoOferta=$tipoOferta;
$this->precioOferta=$precioOferta;
$this->descripcionOferta=$descripcionOferta;
parent::__construct($id,$prod,$descripcion);
}
//METHODS THAT SELECT FROM THE DATABASE
public static function db_oferta_by_ofertaId($mysqli, $idOferta)
{
$query="SELECT * FROM tb_ofertas WHERE id=".$idOferta;
return self::db_select($mysqli, $query);
}
public static function db_oferta_by_productoId($mysqli, $productoId)
{
$query="SELECT * FROM tb_ofertas WHERE idproducto=".$productoId;
return self::db_select($mysqli, $query);
}
protected static function db_select($mysqli, $query)
{
$result = $mysqli->query($query);
if($result->num_rows > 0)
{
$row=$result->fetch_array(MYSQLI_ASSOC);
//MY PROBLEM IS HERE, BECAUSE I WANT TO ACCESS THE THE PARENTS VARIABLES TO RETURN AN OFERTA CLASS READING ALL THE VALUES FROM THE DATABASE
$producto=parent::db_select_producto_by_id($mysqli,$row['idproducto']);
return new Oferta($row['id'], $row['tipooferta'], $row['preciooferta'], $row['descripcionoferta'], $producto->id, $producto->producto, $producto->descripcion);
}else
return new Oferta(0, 'none', 'none', 'none', 'none', 'none', 'none');
}
As you can see in the method db_select from Oferta class, i cant access to the parents properties once i have readed from the database. i would apreciate any solution, thank you in advance