dongrenshi0889 2014-02-17 15:54
浏览 17
已采纳

如何让我的班级从课堂外使用数组?

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

?>
  • 写回答

3条回答 默认 最新

  • dsa45664 2014-02-17 16:04
    关注

    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";
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭