【问题标题】:how to access parent variables in php如何在php中访问父变量
【发布时间】:2014-11-20 03:10:31
【问题描述】:

我不知道以前是否有人问过这个问题,但我一直在寻找它,但我什么也没看到。

我的问题如下:

我有两节课。父类(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 中看到的那样,一旦我从数据库中读取,我就无法访问父属性。 我会感谢任何解决方案,提前谢谢你

【问题讨论】:

  • 您要访问哪个变量?没看懂。。
  • 发生任何错误?
  • 它没有任何错误,这是错误的,因为方法'db_select($mysqli,$query)'应该将从数据库读取的值加载到类的变量中。在父类中有效,但在 Oferta 中无效,因为我不知道如何执行 Producto::db_select() 方法并在子类中使用返回值
  • 我想做的是这样的:
  • 当它从我的数据库中读取时,我得到以下结果: Oferta Object ( [idOferta:protected] => [tipoOferta:protected] => [precioOferta:protected] => [descripcionOferta:protected] = > [id:protected] => 2 [producto:protected] => la mejon [descripcion:protected] => 20.000 ) 前 4 个变量没有值,但我不知道为什么。我已经尝试了一切并检查了代码 1000 次。谁能帮帮我???

标签: php class oop parent extend


【解决方案1】:

您可以通过在 $this 对象上调用它来访问您要扩展的类的方法:

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

【讨论】:

  • 不,抱歉,但它不起作用,因为我不能在静态方法中使用 $this。
  • 抱歉,应该已经发现了。 self:: 和 parent:: 都应该工作。您确定根本没有错误,您是否打开了错误报告和/或检查了错误日志文件?我已经在本地对其进行了测试,对我来说效果很好。
  • 是的,我试过了,但还是不行,我还不知道该怎么办。我认为关键在于保留字 parent 和 self,但我的逻辑有问题。
  • 我做了一些改动,但还是不行。它没有给我任何错误,但我仍然没有得到我想要的。
【解决方案2】:

上面写的代码是对的,问题是我在 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);
}

如您所见,现在函数已正确编写。

【讨论】:

    猜你喜欢
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多