2014-02-25 47 views
2

我可能有一個非常簡單的問題,但我現在不知道如何解決它。也許我只是忘了一些東西,但我需要你的幫助。繼承變量不起作用

我有模式:

<?php 

class Model 
{ 
    protected $storage; 
    public function __construct() 
    { 
    $this->storage = $_SERVER["DOCUMENT_ROOT"] . "/mvc/images/"; 
    if(!file_exists($this->storage)) 
    { 
     mkdir($this->storage, 0777); 
    } 
    } 
    public function storageHandler() 
    { 
    if(count(glob($this->storage."/*")) === 0) 
     { 
     echo "Žiadne kategórie"; 
     } 
    else 
    { 
     $iterator = new DirectoryIterator($this->storage); 
     return $iterator; 
    } 
    } 

} 

?> 

和型號類別,這是從模型繼承。

<?php 
require_once($_SERVER["DOCUMENT_ROOT"] . "/mvc/model/Model.php"); 
    class Category extends Model 
    { 
    private $photos_count; 
    public function __construct($data) 
    { 
     if(isset($data)) 
     { 
      $gallery_path = $this->storage . $data; 
      if(!file_exists($this->$storage .$data)) 
      { 
      mkdir($tihs->storage . $data, 0700); 
      header("Location: /mvc/"); 
      } 
      else 
      { 
      echo "Kategória už existuje."; 
      } 
     } 
    } 


    } 

?> 

我希望在我的Category類中使用Model中的$ storage變量。我應該怎麼做?我知道某種方式,但它不會是最好的。有什麼解決方案可以做出這種好的方式嗎?

+0

比錯字其他,這有什麼錯你這樣做的方式嗎? – Barmar

回答

2

bes雷的IDE的答案,你需要你在你的類類使用parent::__construct()像這樣

class Category extends Model 
    { 
    private $photos_count; 
    public function __construct($data) 
    { 
     parent::__construct(); 
     //rest of the code 
2

的保護屬性$存儲可用的子類,但$this拼寫錯在你的examp:

mkdir($tihs->storage . $data, 0700); 

它應該是:

$this->storage ... 

而且,notedby馬姆杜,你需要調用子構造函數初始化:

public function __construct($data) 
{ 
    parent::_construct(); 
    ...rest of code...