簡體   English   中英

如何在php中訪問父變量

[英]how to access parent variables in php

我不知道是否曾經問過這個問題,但是我一直在尋找它,卻什么也看不到。

我的問題如下:

我有兩節課。 父類(Products.php)包含以下代碼:

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');
    }

子類(Oferta.php)從Producto.php擴展而來,包含以下代碼:

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');
    }

正如您從Oferta類的db_select方法中看到的那樣,一旦我從數據庫中讀取了數據,就無法訪​​問父級屬性。 我會很感激任何解決方案,在此先感謝您

您可以通過在$ this對象上調用來訪問擴展類的方法:

$this->db_select_producto_by_id($mysqli,$row['idproducto']);

上面寫的代碼是正確的,問題是我在Oferta類的構造函數中只有一個簡單的下划線 構造函數的名稱前需要兩個下划線

Oferta的構造應如下所示:

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);
}

如您所見,現在該函數已正確編寫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM