douwen3362 2013-03-06 19:34
浏览 40

if语句和函数参数

I have a function I have to fill out, and the only way I can see doing it is a mess. The function I have is:

public function($keys = array(), $pages = array()){}

Essentially the goal is:

  • If keys are an array and each key is not set, do something.
  • If Keys are not an array and are not set, do something.

The same concept goes for pages.

so that's four if statements, then we do:

If key is array but pages is not, if pages is array but key is not. if both are arrays....

What I have so far is:

public function sidebar($keys = array(), $pages = array()){
    $builder = AisisCore_Factory_Pattern::create('AisisCore_Template_Builder');

    if(is_array($keys)){
        foreach($keys as $key){
            if(!$builder->get_specific_option($key)){
                get_sidebar();
            }
        }
    }else{
        if(!$builder->get_specific_option($keys)){
            get_sidebar();
        }
    }

    if(is_array($pages)){
        foreach($pages as $page){
            if(!$page){
                get_sidebar();
            }
        }
    }else{
        if(!$pages){
            get_sidebar();
        }
    }

}

You can see its quickly becoming a mess. get_specific_option will check if that key exists. arrays are done like: array('key', 'secondKey') and so on. pages are done as such: array(is_page(), is_category()) - each of those returns true or false, hence the boolean check. get_sidebar() will get the sidebar if, say for example, a key is not set, or a page does not equal true.

I am not sure how to create this function with out it being a disaster. Can some one help?

  • 写回答

2条回答 默认 最新

  • donglinyi4313 2013-03-06 19:39
    关注

    You can remove A LOT of complexity by always converting to an array (quick one-liner)

    $keys = is_array($keys) ? $keys : ($keys ? array($keys) : array());
    $pages = is_array($pages) ? $pages : ($pages ? array($pages) : array());
    

    Your function now:

    public function sidebar($keys = array(), $pages = array()){
        $builder = AisisCore_Factory_Pattern::create('AisisCore_Template_Builder');
        $keys = is_array($keys) ? $keys : ($keys ? array($keys) : array());
        $pages = is_array($pages) ? $pages : ($pages ? array($pages) : array());
    
        foreach ($keys as $key) {
            if (!$builder->get_specific_option($key)) {
                get_sidebar();
            }
        }
    
        foreach ($pages as $page) {
            if (!$page) {
                get_sidebar();
            }
        }
    }
    

    This seems like a lot of potential sidebar inclusions. Just saying. But, you can pass NULL for either of the two parameters and it won't run the foreach contents at all.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。