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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀