2011-11-20 23 views
1

我正在PHP中通過Facebook Graph API使用OOP創建一個Student對象。我的問題是,並非所有用戶都在FB上共享相同數量的數據,因此如果某個特定用戶未列出在該對象中實例化的某些變量,則會收到未定義的變量消息。爲此做好準備的最佳方法是什麼,即創建一個對象,無論用戶是否共享對象中的所有數據?代碼如下:使用FB Graph API的OOP PHP

<?php 
require_once('class.Student.php'); 

$name = $user_profile['name']; 
$hometown = $user_profile['hometown']['name']; 
$location = $user_profile['location']['name']; 
$birthday = $user_profile['birthday']; 
$highschool = $user_profile['education'][0]['school']['name']; 
$hsgrad = $user_profile['education'][0]['year']['name']; 
$collegeid = $user_profile['education'][1]['school']['id']; 
$college = $user_profile['education'][1]['school']['name']; 
$majorid = $user_profile['education'][1]['concentration'][0]['id']; 
$major = $user_profile['education'][1]['concentration'][0]['name']; 
$grad = $user_profile['education'][1]['year']['name']; 
$company = $user_profile['work'][0]['employer']['name']; 
$jobtitle = $user_profile['work'][0]['position']['name']; 
$startdate = $user_profile['work'][0]['start_date']; 
$interest = $interests['data'][0]['name']; 
$interestid = $interests['data'][0]['id']; 

$objStudent = new Student($name,$hometown,$location,$birthday,$highschool,$hsgrad,$collegeid,$college,$majorid,$major,$grad,$company,$jobtitle,$startdate,$interest,$interestid); 

?> 

和類本身:

<?php 

class Student { 

public $name; 
public $hometown; 
public $location; 
public $birthday; 
public $highschool; 
public $hsgrad; 
public $collegeid; 
public $college; 
public $majorid; 
public $major; 
public $grad; 
public $company; 
public $jobtitle; 
public $startdate; 
public $interest; 
public $interestid; 


    public function __construct($name,$hometown,$location,$birthday,$highschool,$hsgrad,$collegeid,$college,$majorid,$major,$grad,$company,$jobtitle,$startdate,$interest,$interestid) { 
     $this->name = $name; 
     $this->hometown = $hometown; 
     $this->location = $location; 
     $this->birthday = $birthday; 
     $this->highschool = $highschool; 
     $this->hsgrad = $hsgrad; 
     $this->collegeid = $collegeid; 
     $this->college = $college; 
     $this->majorid = $majorid; 
     $this->major = $major; 
     $this->grad = $grad; 
     $this->company = $company; 
     $this->jobtitle = $jobtitle; 
     $this->startdate = $startdate; 
     $this->interest = $interest; 
     $this->interestid = $interestid; 
    } 

我知道如何在類的函數處理這個問題,使用簡單isset如:

function goalRecommender() { 

     if (isset($this->interest)) { 
      if ($this->interest =='Computer Science') { 
      echo "<p>Based on your interest in Computer Science, we recommend working in the software industry. Furthermore, your interest in user interface design would be thoroughly put to use at Facebook, one of the fastest growing technology companies in the world. If you would like to pursue another goal, 
      search our database to the right or click one of the 'popular goal' links.<p>"; 
      } 
      elseif ($this->interests =='Finance') { 
       echo "<p>You're interested in Finance</p>"; 
      } 
      elseif ($this->interests =='Film') { 
       echo "<p>You're interested in Film</p>"; 
     } 
      elseif ($this->interests =='Marketing') { 
       echo "<p>You're interested in Marketing</p>"; 
      } else { 
       echo "<p>Choose a goal.</p>"; 
      } 
     } else { 
       echo "<p>What are you interested in? 
       <select> 
       <option>Finance</option> 
       <option>Marketing</option> 
       <option>Film</option> 
       <option>Software</option> 
       </select></p>"; 
     } 
    } 

但我只是在PHP中學習OOP,並且不確定如何在實例化對象時做到這一點。任何指導將受到真誠的讚賞。

回答

0

有很多關於你的類:

  • 讓你的學生構造帶的陣列。這將減少可變調整的痛苦,特別是如果您最終延長Student澄清:而不是通過名稱,學院,家鄉,生日等,你傳遞一個具有所有相同信息的數組。

    $name = $user_profile['name']; 
    $hometown = $user_profile['hometown']['name']; 
    
    //becomes 
    
    $user['name'] = $user_profile['name']; 
    $user['hometown'] = $user_profile['hometown']['name']; 
    
    • 它的速度更快。任何參數的成本都不是很高,但是當你有這樣的噸時,它至少可以做出一個標記。
    • 這很容易打電話。你不再需要記住變量的順序。 「姓名,家鄉,地點,生日,還是名字,生日,地點,家鄉?」你有很多變量,沒有辦法容易記住。
    • 它更容易改變。添加一個變量?沒問題,將它添加到數組中並在函數內部檢查它。刪除變量?這很難做,但如果它在一個數組中,則不會。

      public function __construct(
          $name, 
          $hometown, 
          $location, 
          $birthday, 
          $highschool, 
          $hsgrad, 
          $collegeid, 
          $college, 
          $majorid, 
          $major, 
          $grad, 
          $company, 
          $jobtitle, 
          $startdate, 
          $interest, 
          $interestid) { 
      
      //becomes 
      public function __construct(array $info) { 
      
  • 你的變量應該是protectedprivate,不public這幾乎是行業標準。
    • 你會想提供getter和setter方法,因爲變量是不可訪問的。許多IDE可以爲你做這個。

至於你的錯誤消息:

  • 你需要檢查,以確保在您實際上訪問的變量存在。使用isset()功能如下:

    $hometown = isset($user_profile['hometown']['name']) ? $user_profile['hometown']['name'] : null; 
    
+0

謝謝你的見解。我已經實施了2/3的建議。第一個:「讓你的構造函數接受一個數組」我有點不清楚。當你提到可變調整時,我認爲你指的是圖。就像,如果一個用戶只有大學上市,那麼大學會有一個[0],但如果他們列出了大學和學院,大學是[1]開始。如果是這樣,我怎麼讓構造函數採取一個數組來解決這個問題,我不知道我確切地理解你的意思。 – buttonitup

+0

@MattMcClintock我更新了這個問題,希望更清楚。 –

+0

當你寫構造(數組$信息)你的意思是構造(數組$用戶)? – buttonitup

相關問題