2014-09-25 6 views
0

이 질문이 전에 물어 본지는 모르지만 나는 그것을 찾고 있었고 아무것도 볼 수 없었다.PHP에서 부모 변수에 액세스하는 방법

내 문제는 다음과 같습니다.

두 클래스가 있습니다. 아버지 클래스 (Products.php는) 다음 코드를 포함

아이 클래스 (Oferta.php가) Producto.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'); 
    } 

: 당신이 볼 수 있듯이

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 메서드에서 일단 데이터베이스에서 읽은 부모 속성에 액세스 할 수 없습니다. 내가 모든 솔루션을 뵙죠 것, 사전에 감사

당신은 단지 $에이 개체를 호출하여 당신이에서 연장하고있는 클래스의 메소드에 액세스 할 수 있습니다
+0

어떤 변수에 액세스하려고합니까? 나는 그것을 얻지 못한다 .. – Zim84

+0

어떤 에러가 발생합니까? –

+0

그것은 'db_select ($ mysqli, $ query)'메소드가 DB에서 읽은 값을 클래스의 변수에로드해야하기 때문에 오류가 없습니다. 아버지 클래스에서는 작동하지만, Oferta에서는 Producto :: db_select() 메서드를 실행하고 자식 클래스에서 retuened 값을 사용하는 방법을 알지 못하기 때문에 doesnt합니다. – galeonweb

답변

0

:

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

아니, 죄송합니다,하지만 그것은 정적 메서드에서 $ this를 사용할 수 없기 때문에 작동하지 않습니다. – galeonweb

+1

죄송합니다. self ::와 parent ::는 모두 작동해야합니다. 오류가 전혀없고, 오류보고 기능을 켜 놓았으며 오류 로그 파일을 검사 했습니까? 나는 그것을 로컬에서 테스트 해본 결과 잘 작동한다. – LS11

+0

네, 시도했지만, 아직 작동하지 않습니다, 나는 아직 무엇을 해야할지 모르겠다. 나는이 열쇠가 부모와 자아라는 보편적 인 단어에 있다고 생각하지만, 내 논리에서 뭔가 잘못되었다고 생각한다. – galeonweb

0
위 writen 코드는

을 맞아, 문제는 내가 단지 하나의 간단한 밑줄 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); 
} 

, 이제 기능이 제대로 기록됩니다.

+0

답변을 수락 할 수 있습니다. –