nukzar / php7new

这个新的 php7 函数

1.0.0 2020-01-28 19:58 UTC

This package is auto-updated.

Last update: 2024-09-10 06:11:36 UTC


README

  1. 在 PHP 7.0 中添加了对返回类型声明的支持。
             public function __construct(int $id)
            {
                   $this->id = $id;
            }
    
             public function getId(): int
             {
                   return $this->id;
              }
    
  2. 在 PHP 7.0 中添加了严格类型检查
           <?php
           declare(strict_types=1);
           
           function sum(int $a, int $b) {
               return $a + $b;
           }
           
           var_dump(sum(1, 2));
           var_dump(sum(1.5, 2.5)); // Fatal error: Uncaught TypeError: Argument 1 passed to sum() must be of the type integer, float given.
    
  3. 在 PHP 7.1 中支持类常量的可见性修饰符
            	private const PRIVATE_CONST = 0;
                    	protected const PROTECTED_CONST = 0;
                    	public const PUBLIC_CONST_TWO = 0;
             
            // Весь список имеет одну область видимости
                    	private const FOO = 1, BAR = 2;
    
  4. 在 PHP 7.2 中在密码哈希中添加了 Argon2 算法
        Argon2 — это современный простой алгоритм, направленный на высокую 
        скорость заполнения памяти и эффективное использование нескольких вычислительных блоков.
        
        echo 'Хеш Argon2i: ' . password_hash('rasmuslerdorf', PASSWORD_ARGON2I);
        
        // Хеш Argon2i:
        // $argon2i$v=19$m=1024,t=2,p=2$YzJBSzV4TUhkMzc3d3laeg$zqU/1IN0/AogfP4cmSJI1vc8lpXRW9/S0sYY2i2jHT0
    
  5. 在 PHP 7.3 中添加了 is_countable() 函数
        if (is_countable($someVar)) {  
          echo count($someVar);  
        }  else {
          echo ‘$someVar is not countable’;  
        }
    
  6. 在 PHP 7.3 中添加了 array_key_first() 和 array_key_last() 函数
        // usage of an associative array
        $array = ['a' => 1, 'b' => 2, 'c' => 3];
        $firstKey = array_key_first($array);
        assert($firstKey === 'a');
        
        // usage of a numeric array
        $array = [1 => 'a', 2 => 'b', 3 => 'c'];
        $firstKey = array_key_first($array);
        assert($firstKey === 1);
    
  7. 在 PHP 7.4 中添加了类类型属性
            class Bar
            {
                public string $name;
                protected ?int $amount;
                private Foo $foo;
            }
    
    </li>
    

安装

git clone https://github.com/TkachukV/php7new.git

许可协议

此项目遵循 MIT 许可协议 - 详细信息请参阅 LICENSE 文件