dppn67180 2012-08-25 03:34
浏览 22
已采纳

表格代码在飞行和eval()它可重复的代码块 - PRO&CONTRA?

I was reading sourses of OpenCart and phpBB engines and noticed that there are lot of strings (sometimes full screen listings) with repeated code, which differs only in one parameter. Such as:

$this->data['button_cart'] = $this->language->get('button_cart');
$this->data['button_wishlist'] = $this->language->get('button_wishlist');
$this->data['button_compare'] = $this->language->get('button_compare');
$this->data['button_continue'] = $this->language->get('button_continue');

I am thinking about using function for generating code using paterns, and then eval() it.

Some such function:

function CodeGenerator($patern, $placements_arr){
    $echo_str = '';
    foreach($placements_arr as $placement){
        $echo_str .= str_replace('=PATERN=', $placement, $patern);
    }
    if(substr($echo_str, -1)!==';'){
        $echo_str .= ';'; # for correct eval() working
    }
    return $echo_str;
}

And then for large repeated blocks of code with same patern:

$patern = "$this->data['=PATERN='] = $this->language->get('=PATERN=');";
$placements_arr = array('button_cart', 'button_wishlist', 'button_compare', 'button_continue');
$echo_str = CodeGenerator($patern, $placements_arr);
eval($echo_str);

I want to understand PRO and CONTRA of such design, because I am thinking about using such design in my future development.

The only problem I see here now - a bit more slow execution. Any others?

  • 写回答

2条回答 默认 最新

  • doula2426 2012-08-25 03:42
    关注

    Well for the block of code you have shown you could rewrite it like this

    $params = array('button_cart', 'button_wishlist', 'button_compare', 'button_continue');
    
    foreach($params as $param)
       $this->data[$param] = $this->language->get($param);
    

    You are writing out the parameters anyways, so I cannot see one benefit to your code over something like what I have shown above. Plus this is only 3 lines of code vs 11 of yours, and mine is instantly readable

    in 99.9% of the code you write, you can write it without eval. There are some corner cases where eval makes sense, but in my 5 years of coding php so far I have used it maybe one or 2 times, and if I went back to the code I could probably rewrite it so It didn't.

    If I had to maintain a project with code that you wrote, I would be tearing my hair out. Just look at what OpenCart wrote, and look at what you wrote. Which one is easier to understand? I actually have to look at your code a few times to understand what it is doing, I can skim over the OpenCart code and instantly understand what is happening.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?