zoukankan      html  css  js  c++  java
  • 变长参数处理

    1. func_get_args

    func_num_args();
    func_get_args();
    

    示例

    function test() {
      var_dump(func_num_args());
      var_dump(func_get_args());
    }
        
    $params = array(
      10,
      'glop',
      'test',
    );
    
    // invoke
    
    call_user_func_array('test', $params);
    

    2. using ... operator

    Notice: PHP version > 5.6

    meanwhile you can set the type of parameters

    function addDateIntervalsToDateTime( DateTime $dt, DateInterval ...$intervals )
    {
        foreach ( $intervals as $interval ) {
            $dt->add( $interval );
        }
        return $dt;
    }
    
    // one
    addDateIntervaslToDateTime( new DateTime, new DateInterval( 'P1D' ), 
            new DateInterval( 'P4D' ), new DateInterval( 'P10D' ) );
            
    // two
    $list = [new DateInterval( 'P1D' ), new DateInterval( 'P4D' ), new DateInterval( 'P10D' )];
    addDateIntervaslToDateTime(new DateTime, ...$list);
    

    In php 7

    function do_something(int ...$all_the_others) { /**/ }
    

    3. use ReflectionClass

    $req = new Request();
    $ps = [$req];
    
    $ref = new ReflectionClass(RequestCollection::class);
    $o = $ref->newInstanceArgs($ps);
    
    // same as follow
    $o = new RequestCollection(...$ps);
    
  • 相关阅读:
    查看数据库表中的数据
    exec和execsql
    CPI
    百度硬盘可以检索的字节测试
    HDU2095
    Vigenere密码
    斌神无所不能
    HDU p1017
    POJ1316
    head区的代码详解
  • 原文地址:https://www.cnblogs.com/brookin/p/7760787.html
Copyright © 2011-2022 走看看