2014-02-17 127 views
1

我目前正試圖回到面向對象編程中如何讓我的數組在我的類中?全球並不縫合它。如何讓我的課程使用課堂外的數組?

<? 
$systems = file_get_contents('https://api.eveonline.com/map/Sovereignty.xml.aspx'); 
$systems = explode("<row ",$systems); 
//print_r($systems); 
for ($i = 1; $i <= count($systems); $i++) { 

    //system name 
    $systemnames=explode("solarSystemName=",$systems[$i]); 
    $systemnames=explode('"',$systemnames[1]); 
    $systemnames=$systemnames[1]; 

    //system id 
    $systemid=explode("solarSystemID=",$systems[$i]); 
    $systemid=explode('"',$systemid[1]); 
    $systemid=$systemid[1]; 

    $systembyid[$systemid]=$systemnames; 
    $systembyname[$systemnames]=$systemid; 
} 

class Systems{  

    public function __construct() 
    { 
     global $systembyid; 
     global $systembyname; 
    } 

    function getSystems($system) 
    { 
     if (is_numeric($system) && $systembyid[$system]) { 
      return $systembyid[$system]; 
     } 
     elseif($systembyname[$system]){ 
      return $systembyname[$system]; 
     } 
     else{ 
      return "Error: Invalid system id or name"; 
     } 
    } 
} 

?> 

回答

0

嘗試將值傳遞給像這樣的構造函數,如果您使用&,那麼您只是傳遞引用而不是製作整個數組的副本。

class Systems{  

    private $sysyembyid; 
    private $systembyname; 

    public function __construct(&$systembyid, &$systembyname) 
    { 
     $this->systembyid = $systembyid; 
     $this->systembyname = $systembyname; 
    } 

    function getSystems($system){ 
     if(is_numeric($system) && $this->systembyid[$system]){ 
     return $this->systembyid[$system]; 
    } 
    elseif($this->systembyname[$system]){ 
      return $this->systembyname[$system]; 
     } 
     else{ 
      return "Error: Invalid system id or name"; 
     } 
    } 
} 
0

我更喜歡使用依賴注入。依賴注入是當你通過構造函數注入對象的依賴關係時。這可以確保該對象在創建時具有其依賴關係。

class Systems { 
    protected $systembyid; 
    protected $systembyname; 

    public function __construct($systembyid, $systembyname) 
    { 
     $this->systembyid = $systembyid; 
     $this->systembyname = $systembyname; 
    } 

    public function getSystems($system) { 
     //Access them with $this-> like below 
     $this->systembyid[$system]; 
     $this->systembyname[$system]; 
    } 
} 

如果你希望能夠通過爲指定的參數修改$systembyid$systembyname類的外面,看到了類中的變化,你可以通過引用__construct()相反,引用:

public function __construct(&$systembyid, &$systembyname) 
{ 
    $this->systembyid = $systembyid; 
    $this->systembyname = $systembyname; 
} 

或者,您也可以通過ŧ下襬作爲您的getSystems()方法的參數。

class Systems() { 
    public function getSystems($system, $systembyid, $systembyname) { 
     //Do stuff 
    } 
} 

主要缺點這種方法是,你總是必須將它們作爲參數傳遞給方法,而該方法的簽名可以變得很長。

0

要麼你需要在你使用它的功能與VAR使用global關鍵字,在這種情況下getSystems()(壞)或者將它們傳遞到構造函數或您使用它們的功能,或將它們設置:

也許最常見的情況:

public function __construct($s1, $s2) 
    { 
     $this->systembyid = $s1 
     $this->systembyname = $s2 
    } 
//then use $this->systembyid etc in other functions 

或者更好的是,爲什麼不把所有的處理代碼的功能關閉類像processSystems()並設置有瓦爾:

public function processSystems($file) { 
    $systems = file_get_contents($file); 
    $systems = explode("<row ",$systems); 
    //print_r($systems); 
    for ($i = 1; $i <= count($systems); $i++) { 

     //system name 
     $systemnames=explode("solarSystemName=",$systems[$i]); 
     $systemnames=explode('"',$systemnames[1]); 
     $systemnames=$systemnames[1]; 

     //system id 
     $systemid=explode("solarSystemID=",$systems[$i]); 
     $systemid=explode('"',$systemid[1]); 
     $systemid=$systemid[1]; 

     $systembyid[$systemid]=$systemnames; 
     $systembyname[$systemnames]=$systemid; 
    } 
    $this->systembyid = $systemnames; 
    $this->systembyname = $systemid; 
} 

除此之外,我會說看看simple_xmlDOM爲XML解析。

此外,您正在存儲每個陣列中完全相同的數據。只需使用一個,然後查找關鍵字或值。