Abusing PHP’s parser

Did you know?

2009/04/01
Previously On Atomized:

Discussion

Another one: [code]if ($x = foo()) {.... do_something_with($x); }[/code] – IDEs do hate it (marking as a warning), but it’s perfectly legal too. Doing this you save CPU cycles (foo is called once instead n-times), you save your time, because referencing $x is shorter than referencing foo() or my_long_classname::my_long_method($my_first_param, $my….). It is NO readibility issue, because every decent coder knows, we TEST if $x evaluates to true, not compare here. “==” stands for equality, and “===” for true equality. Well, it’s just another version of [code]while ($record = $sql->fetch_object()) { ... }[/code].

The last one, but pretty cool:

[code]if (f($x) && g($x) && h($x))...[/code] – g() and h() won’t be called if f() returns false.
[code]if (f($x) || g($x) || h($h))...[/code] – g() and h() won’t be called if f() returns true.

And if you really want to abuse PHP parser call eval() a lot, if you want to abuse it badly – call it instead of call_user_func_array() :)

What PHP parser will swallow is [code]my_class::my_method(func_get_args(), $x, $y...[/code] But don’t try this: [code]my_class::my_method($x, func_get_args()...[/code] – well, try, see what happens :)

Harry
2009/12/18

Participate