【发布时间】: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