2016-08-04 25 views
3

我正在使用PHP OOP定製CMS,這實際上是我的第一個項目,它是用面向對象編程製作的,所以我沒有太多的經驗。基本上,我有一個名爲Site.class.php類在MySQL數據庫中的一個表中檢索數據是這樣的:PHP中的未定義屬性錯誤信息OOP

<?php 
class Site 
{ 
    public $id,$site_name,$site_title,$site_url,$site_tags,$site_desc; 
    public function __construct() 
    { 
     $this->db = new Connection(); 
     $this->db = $this->db->dbConnect(); 
    } 
    public function getSite($name) 
    { 
     if(!empty($name)) 
     { 
      $site = $this->db->prepare("select * from admins where site_name = ?"); 
      $site->bindParam(1,$name); 
      $site->execute(); 
      while($row = $site->fetch()) 
      { 
       $this->id   = $row['id']; 
       $this->site_name = $row['site_name']; 
       $this->site_title = $row['site_title']; 
       $this->site_url  = $row['site_url']; 
       $this->site_tags = $row['site_tags']; 
       $this->site_desc = $row['site_desc']; 
      } 
     } 
     else 
     { 
      header("Location: maint/php/includes/errors/005.php"); 
      exit(); 
     } 
    } 
    public function getID() 
    { 
     return $this->id; 
    } 
    public function getSiteName() 
    { 
     return $this->site_name; 
    } 
    public function getSiteTitle() 
    { 
     return $this->site_title; 
    } 
    public function getSiteUrl() 
    { 
     return $this->site_url; 
    } 
    public function getSiteTags() 
    { 
     return $this->site_tags; 
    } 
    public function getSiteDesc() 
    { 
     return $this->site_desc; 
    } 
} 
?> 

我已經包含在被稱爲settings.php並把它稱爲這樣另一個文件這個文件:

$siteSet = new Site(); 
$siteSet->getSite("Daygostar"); 

然後我試圖呼應出來的變量是這樣的:

<div class="box-body"> 
       <div class="row"> 
        <div class="col-md-6"> 
         <div class="form-group"> 
          <label for="usr">Site Name:</label> 
          <input type="text" class="form-control" id="usr" disabled='disabled' value="<?php echo $siteSet->getSiteName; ?>"> 
         </div> 
         <div class="form-group"> 
          <label for="usr">User URL:</label> 
          <input type="text" class="form-control" id="usr" disabled='disabled' value="<?php echo $siteSet->getSiteUrl; ?>"> 
         </div> 
        </div> 
       </div> 
      </div> 

但問題是,每當我把這個FIL E,我收到此錯誤信息:

未定義的屬性:網站:: $ getSiteName

未定義的屬性:網站:: $ getSiteUrl

我不知道發生了什麼真的出錯了,因爲我已經正確地編碼了一切!所以,如果你知道如何解決這個問題,請讓我知道,我真的很感激..在此先感謝。

+0

備註:如果您執行'__call($ name)'方法並刪除所有調用變量的方法,您將自行保存幾行。在魔術方法中,你可以做'$ split = preg_split('/(?= [A-Z])/',$ name); $ var = strtolower($ split [1]。'_'。$ split [2]);返回$ this - > {$ var};'這樣當你執行'$ obj-> getSiteName();'它會動態地返回'$ this-> site_name'。讓您不用編寫大量機械地完成同樣任務的方法。需要思考的東西。 – Rasclatt

+0

我認爲你很快就會與OOP一起跳躍。去吧! –

回答

2

這些都是兩種方法。您需要將()添加到它們的末尾以調用該方法。

<div class="box-body"> 
       <div class="row"> 
        <div class="col-md-6"> 
         <div class="form-group"> 
          <label for="usr">Site Name:</label> 
          <input type="text" class="form-control" id="usr" disabled='disabled' value="<?php echo $siteSet->getSiteName(); ?>"> 
         </div> 
         <div class="form-group"> 
          <label for="usr">User URL:</label> 
          <input type="text" class="form-control" id="usr" disabled='disabled' value="<?php echo $siteSet->getSiteUrl(); ?>"> 
         </div> 
        </div> 
       </div> 
      </div> 
相關問題