表格代码在飞行和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?

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.

Maintainability - if that's a word - might be a small concern. I would despise such a construct because it seems unnecessarily complex. I have inherited many - a - poorly designed php sites working as a web developer and in just about zero cases can I recall it being considered a nuisance to have to spool through a list of var assignments like above. However, I would become infuriated by having to deal with weird lazy functions that attempt to escape the banalities of repetitive typing.

In the end you're talking about fractions of a second for processing so that's hardly an argument for doing something like this. And if the microseconds are a concern, use a caching mechanism to write to flat text and wipe out all redundant processing all together.

Hey, my 2 cents. If it's your project and you don't expect anyone else to maintain it then knock yourself out.