CodeSOD: Metaception

This post was originally published on this site

The Daily WTF

Meta-programming- programs which generate programs- is a delightful hobby, but usually shouldn’t be used in production code. Usually. I mean, if you’re working in LISP, 90% of your program is going to be macros.

But if you’re using PHP and JavaScript, there’s good odds that someone you work with has decided to combine these two tastes together to create something nobody wants to taste.

This is a depressingly common anti-pattern, usually seen in the form of JavaScript code which looks like: if (1 == 2)- once upon a time one or both of those values were PHP variables, but we only see the resulting generated code.

A more extreme version happened to our anonymous submitter.

<div class=”w-box-content”> <? table(array( //… ‘options’=> array( <script type=”text/javascript”> {if $doc_status==”edit”} $(“#doc-{$id}”).closest(“tr”).find(“td,a”).addClass(“text-danger”); {/if} </script>’ ) )); ?> </div>

We start with some HTML which wraps some PHP code. The PHP code embeds an HTML

To read the full article click on the 'post' link at the top.