2014-02-17 226 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解析。

此外,您正在存储每个阵列中完全相同的数据。只需使用一个,然后查找关键字或值。