Code-Golf:一行PHP语法 [英] Code-Golf: one line PHP syntax

查看:94
本文介绍了Code-Golf:一行PHP语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



PHP的语法有一些漏洞,有时在程序员的开发中会遇到一些漏洞。这可能会导致很多挫折,因为这些语法漏洞似乎无缘无故地存在。例如,在同一行( func1()[100] 不是有效的PHP语法)中,不能轻松创建数组并访问该数组的任意元素。解决此问题的方法是使用临时变量并将语句分成两行,但有时会导致代码非常冗长,笨拙。



挑战



我知道这些洞中的一些(我确信还有更多)。即使想出一个解决方案也很难,更不用说代码高尔夫球风格了。 Winner是所有四个Syntax Holes中字符数最少的人。



规则


  1. 语句必须为以下格式的一行: $ output = ...; ,其中 .. 。不包含任何; 的。

  2. 仅使用标准库函数或 eval 允许)

  3. 语句与非工作语法的假设功能(甚至在失败的情况下)完全相同。

  4. 语句必须运行而不出现任何类型的语法错误,使用 E_STRICT | E_ALL

语法孔
$ b


  1. $ output = func_return_array()[$ key]; - 访问任意偏移量( string
  2. $ output = integer 新的{$ class_base。$ class_suffix}(); - 用于创建新类的任意字符串连接
  3. $ output = { $ func_base。$ func_suffix}(); - 作为函数调用的任意字符串连接
  4. $ output = func_return_closure()();


  5.  <?php 

    error_reporting(E_ALL | E_STRICT);

    // 1
    函数func_return_array(){return array(0 =>'hello'); }
    $ key = 0;

    $ output = $ {!$ {''} = func_return_array()} [$ key];

    echo'1:'。 $输出。 \\\
    ;


    // 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);

    $ b $ // $ 4
    函数func_return_closure(){
    返回函数(){
    return'这是闭包';
    };
    }

    $ output = $ {!$ {''} = func_return_closure()}();

    echo'4:';
    var_dump($ output);

    输出:

      1:hello 
    2:object(Thing)#1(0){
    }
    3:array(1){
    [0] =>
    string(5)hello
    }
    4:string(17)这是一个闭包


    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 or eval 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

    解决方案

    The only solution I see involves a temporary variable, so there is some (minimal) namespace pollution. Any way of tightening the temporary variable code would shorten all 4 of these:

    <?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);
    

    Output:

    1: hello
    2: object(Thing)#1 (0) {
    }
    3: array(1) {
      [0]=>
      string(5) "hello"
    }
    4: string(17) "This is a closure"
    

    这篇关于Code-Golf:一行PHP语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆