美文网首页PHP
PHP7 新语法总结,更新7.2注意事项

PHP7 新语法总结,更新7.2注意事项

作者: 飞凡的陀螺 | 来源:发表于2018-01-27 14:07 被阅读1185次
    1. 标量类型声明
    function setAge(int $age) {
      var_dump($age);
    }
    // 要求传入参数是整型
    // echo setAge('dwdw');
    // Fatal error: Uncaught TypeError: Argument 1 passed to setAge() must be of the type integer, string given...
    // 注意这么写不会报错
    echo setAge('1');
    
    1. 返回值类型声明
    class User {}
    
    function getUser() : array {
      return new User;
    }
    // Fatal error: Uncaught TypeError: Return value of getUser() must be of the type array, object returned
    var_dump(getUser());
    // 改成下面不会报错
    function getUser() : User {
      return new User;
    }
    
    // 如果返回的类型不对
    function getUser() : User {
        return [];
    }
    // 会报
    // Fatal error: Uncaught TypeError: Return value of getUser() must be an instance of User, array returned 
    
    
    // 再来个interface的例子, 执行下面的不会报错
    interface SomeInterFace {
        public function getUser() : User;
    }
    
    class User {}
    
    class SomeClass implements SomeInterFace {
        public function getUser() : User {
            return [];
        }
    }
    // 但是当调用的时候才会检查返回类型
    // Fatal error: Uncaught TypeError: Return value of SomeClass::getUser() must be an instance of User, array returned
    (new SomeClass)->getUser();
    
    1. 太空船操作符(组合比较符)

    太空船操作符用于比较两个表达式。当$a小于、等于或大于$b时它分别返回-1、0或1

    // Integers
    echo 1 <=> 1; // 0
    echo 1 <=> 2; // -1
    echo 2 <=> 1; // 1
    
    // 在usort自定义排序方法中很好用
    
    $arr = ['c', 'd', 'b', 'a'];
    // ['a', 'b', 'c', 'd']
    usort($arr, function($a, $b) {
      return $a <=> $b;
    });
    
    1. Null合并运算符

    PHP7之前:
    isset($_GET['id']) ? $_GET['id'] : 'err';
    PHP7之后:
    $_GET['id'] ?? 'err';

    1. use 批量声明

    PHP7之前:

    use App\Model\User;
    use App\Model\Cart;
    use App\Model\Base\BaseUser;
    

    PHP7之后:

    use App\Model\{
      User,
      Cart,
      Base\BaseUser
    };
    
    1. 匿名类
    class SomeClass {}
    interface SomeInterface {}
    trait SomeTrait {}
    
    var_dump(new class(10) extends SomeClass implements SomeInterface {
        private $num;
    
        public function __construct($num)
        {
            $this->num = $num;
        }
    
        use SomeTrait;
    });
    // 输出
    object(class@anonymous)[1]
      private 'num' => int 10
    

    7.2 之后要注意的地方

    1. each 函数 在php7.2已经设定为过时
    <?php
    $b = array();
    each($b);
    
    // Deprecated:  The each() function is deprecated. This message will be suppressed on further calls
    

    兼容方法

    function fun_adm_each(&$array){
       $res = array();
       $key = key($array);
       if($key !== null){
           next($array); 
           $res[1] = $res['value'] = $array[$key];
           $res[0] = $res['key'] = $key;
       }else{
           $res = false;
       }
       return $res;
    }
    
    1. count 函数在php7.2将严格执行类型区分. 不正确的类型传入, 会引发一段警告.
      count方法使用非常广泛,升级7.2后多注意测试。
    <?php
    
    count('');
    
    // Warning:  count(): Parameter must be an array or an object that implements Countable
    

    兼容方法

    function fun_adm_count($array_or_countable,$mode = COUNT_NORMAL){
        if(is_array($array_or_countable) || is_object($array_or_countable)){
            return count($array_or_countable, $mode);
        }else{
            return 0;
        }
    }
    
    1. create_function创建匿名方法不鼓励使用。

    参考:

    https://laracasts.com/series/php7-up-and-running
    http://php.net/manual/zh/language.oop5.anonymous.php
    https://www.cnblogs.com/phpnew/p/7991572.html

    相关文章

      网友评论

        本文标题:PHP7 新语法总结,更新7.2注意事项

        本文链接:https://www.haomeiwen.com/subject/dflnaxtx.html