Ken*_*ins 10 php arrays syntax code-golf function
说明
PHP在其语法中有一些漏洞,偶尔在开发过程中,程序员会介入它们.这可能会导致很多挫折,因为这些语法漏洞似乎无缘无故地存在.例如,一个人不能轻易地创建一个数组并在同一行上访问该数组的任意元素(func1()[100]不是有效的PHP语法).此问题的解决方法是使用临时变量并将语句分成两行,但有时这会导致非常详细,笨重的代码.
挑战
我知道其中一些漏洞(我确信还有更多漏洞).甚至很难提出解决方案,更不用说代码高尔夫风格了.获胜者是所有四个语法孔中总字符数最少的人.
规则
$output = ...;,哪里...不包含任何;.eval允许)E_STRICT | E_ALL.语法孔
$output = func_return_array()[$key];- 访问函数返回数组的任意偏移量(string或integer)$output = new {$class_base.$class_suffix}(); - 用于创建新类的任意字符串连接$output = {$func_base.$func_suffix}(); - 任意字符串连接被称为函数$output = func_return_closure()(); - 调用从另一个函数返回的闭包小智 8
我看到的唯一解决方案涉及一个临时变量,因此存在一些(最小的)命名空间污染.任何收紧临时变量代码的方法都会缩短所有这4个:
<?php
error_reporting(E_ALL | E_STRICT);
// 1
function func_return_array() { return array(0 => 'hello'); }
$key = 0;
$output = ${!${''}=func_return_array()}[$key];
echo '1: ' . $output . "\n";
// 2
class Thing {}
$class_base = 'Thi'; $class_suffix = 'ng';
$output = new ${!${''}=$class_base.$class_suffix}();
echo '2: ';
var_dump($output);
// 3
$func_base = 'func_'; $func_suffix = 'return_array';
$output = ${!${''}=$func_base.$func_suffix}();
echo '3: ';
var_dump($output);
// 4
function func_return_closure() {
return function() {
return 'This is a closure';
};
}
$output = ${!${''}=func_return_closure()}();
echo '4: ';
var_dump($output);
Run Code Online (Sandbox Code Playgroud)
输出:
1: hello
2: object(Thing)#1 (0) {
}
3: array(1) {
[0]=>
string(5) "hello"
}
4: string(17) "This is a closure"
Run Code Online (Sandbox Code Playgroud)