dqnhfbc3738 2014-09-25 11:03
浏览 79
已采纳

如何在php中访问父变量

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

  • 写回答

2条回答 默认 最新

  • duankuiyu4618 2014-09-25 11:20
    关注

    You can access the method of the class you're extending from by just calling it on the $this object:

    $this->db_select_producto_by_id($mysqli,$row['idproducto']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 MATLAB四叉树处理长方形tif文件
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了