简体   繁体   中英

How do i get my class to use an array from outside the class?

I'm currently trying to get back into object oriented programming How do i get my array inside my class? Global doesn't seam to cut it.

<?
$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";
        }
    }
}

?>

I prefer to use Dependency Injection . Dependency Injection is when you inject your object's dependencies via the constructor. This ensures that the object will have its dependencies at creation.

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];
    }
}

Note If you want to be able to modify $systembyid and $systembyname outside of the class, and see the changes within the class, you can pass references to __construct() instead, by specifying the parameters as references:

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

Alternatively, you can pass them as parameters to your getSystems() method.

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

The main drawbacks with this approach is that you always have to pass them as parameters to the method, and the method signature could get quite long.

You either need to use the global key word with var in the function where you use it, in this case getSystems() (bad) or pass them into the constructor or the function where you use them, or set them:

Probably the most common case:

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

Or better yet, why not put all that processing code in a function off the class like processSystems() and set the vars there:

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;
}

Aside from that, I would say look into simple_xml or DOM for the XML parsing.

Also, you are storing the exact same data in each array. Just use one and either lookup the key or the value.

Try passing the values into the constructor like this, also if you use the & you are just passing a reference and not making a copy of the whole array.

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";
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM