Code-Golf: one line PHP syntax

Posted by Kendall Hopkins on Stack Overflow See other posts from Stack Overflow or by Kendall Hopkins
Published on 2010-06-10T04:14:25Z Indexed on 2010/06/10 4:22 UTC
Read the original article Hit count: 264

Filed under:
|
|
|
|

Explanation

PHP has some holes in its' syntax and occasionally in development a programmer will step in them. This can lead to much frustration as these syntax holes seem to exist for no reason. For example, one can't easily create an array and access an arbitrary element of that array on the same line (func1()[100] is not valid PHP syntax). The workaround for this issue is to use a temporary variable and break the statement into two lines, but sometimes that can lead to very verbose, clunky code.

Challenge

I know of a few of these holes (I'm sure there are more). It is quite hard to even come up with a solution, let alone in a code-golf style. Winner is the person with in the least characters total for all four Syntax Holes.

Rules

  1. Statement must be one line in this form: $output = ...;, where ... doesn't contain any ;'s.
  2. Only use standard library functions (no custom functions allowed)
  3. Statement works identically to the assumed functional of the non-working syntax (even in cases that it fails).
  4. Statement must run without syntax error of any kind with E_STRICT | E_ALL.

Syntax Holes

  1. $output = func_return_array()[$key]; - accessing an arbitrary offset (string or integer) of the returned array of a function
  2. $output = new {$class_base.$class_suffix}(); - arbitrary string concatenation being used to create a new class
  3. $output = {$func_base.$func_suffix}(); - arbitrary string concatenation being called as function
  4. $output = func_return_closure()(); - call a closure being returned from another function

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays