编程语言
首页 > 编程语言> > PHP7.1的几个新特性

PHP7.1的几个新特性

作者:互联网

1、try catch 支持捕获多个异常

  1. #写代码的时候不免要自定义异常,以前捕获多个异常需要这样写
  2. try{
  3. ...
  4. }catch(myException $e) {
  5. ...
  6. }catch(anotherExcpetion $e) {
  7. ...
  8. }catch(Exception $e) {
  9. ...
  10. }
  11. #php7.1可以这么写了
  12. try{
  13. ...
  14. }catch(myException | anotherException $e) {
  15. ...
  16. }catch(Exception $e) {
  17. ...
  18. }
#写代码的时候不免要自定义异常,以前捕获多个异常需要这样写
try{
...
}catch(myException $e) {
...
}catch(anotherExcpetion $e) {
...
}catch(Exception $e) {
...
}

php7.1可以这么写了

try{
...
}catch(myException | anotherException $e) {
...
}catch(Exception $e) {
...
}

 

1、class 支持常量的可见性设置

  1. #以前的类里面的常量不可以设置可见性,例如
  2. class MyClass
  3. {
  4. protected $orderArr;
  5. private $key;
  6. const OUT_TIME = 1000; #这里不能添加修饰
  7. }
  8. #现在你可以为const设置可见性了
  9. class MyClass
  10. {
  11. public $name
  12. protecte $orderArr;
  13. private $key;
  14. protected const OUT_TIME = 1000; #这里可以设置可见性了
  15. }
#以前的类里面的常量不可以设置可见性,例如
class MyClass
{
protected $orderArr;
private $key;
const OUT_TIME = 1000; #这里不能添加修饰
}

现在你可以为const设置可见性了

class MyClass
{
public $name
protecte $orderArr;
private $key;
protected const OUT_TIME = 1000; #这里可以设置可见性了
}

 

3、多变量同时赋值

 

4、放弃mcrypt扩展

  1. #以前用mcrypt写一个加密大概是这样
  2. public function encryptData($str, $key)
  3. {
  4. $block = mcrypt_get_block_size('des', 'ecb');
  5. $pad = \(block - </span><span class="br0">(</span><span class="kw1">strlen</span><span class="br0">(</span><span class="">\)str) % $block);
  6. \(str .= </span><span class="kw1">str_repeat</span><span class="br0">(</span><span class="kw1">chr</span><span class="br0">(</span><span class="">\)pad), $pad);
  7. $res = mcrypt_encrypt(MCRYPT_DES, $key, \(str, MCRYPT_MODE_ECB</span><span class="br0">)</span><span class="">;</span></li><li class=" odd"><span class=""> return </span><span class="kw1">strtoupper</span><span class="br0">(</span><span class="kw1">bin2hex</span><span class="br0">(</span><span class="">\)res));
  8. }
  9. #现在你需要用openssl来替换了
  10. public function encryptData($str, $key)
  11. {
  12. \(res = </span><span class="kw1">openssl_encrypt</span><span class="br0">(</span><span class="">\)str, 'DES-ECB', \(key, OPENSSL_RAW_DATA</span><span class="br0">)</span><span class="">;</span></li><li class=" odd"><span class=""> return </span><span class="kw1">strtoupper</span><span class="br0">(</span><span class="kw1">bin2hex</span><span class="br0">(</span><span class="">\)res));
  13. }
  14. #以前用mcrypt写一个解密大概是这样
  15. public function decryptData($str, $key)
  16. {
  17. \(str = </span><span class="kw1">strtolower</span><span class="br0">(</span><span class="">\)str);
  18. $str = pack('H', $str);
  19. $decrypted = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
  20. \(dec_s = </span><span class="kw1">strlen</span><span class="br0">(</span><span class="">\)decrypted);
  21. \(padding = </span><span class="kw1">ord</span><span class="br0">(</span><span class="">\)decrypted[$dec_s - 1]);
  22. \(decrypted = </span><span class="kw1">substr</span><span class="br0">(</span><span class="">\)decrypted, 0, -$padding);
  23. return \(decrypted;</span></li><li class=" even"><span class=""></span><span class="br0">}</span><span class=""></span></li><li class=" odd"><span class=""></span></li><li class=" even"><span class=""></span></li><li class=" odd"><span class=""></span><span class="co1">#现在也要用openssl替换了</span><span class=""></span></li><li class=" even"><span class=""></span></li><li class=" odd"><span class="">public function </span><span class="kw1">decryptData</span><span class="br0">(</span><span class="">\)str, $key)
  24. {
  25. \(str = </span><span class="kw1">strtolower</span><span class="br0">(</span><span class="">\)str);
  26. $str = pack('H', $str);
  27. \(decrypted = </span><span class="kw1">openssl_decrypt</span><span class="br0">(</span><span class="">\)str,'DES-ECB', $key,OPENSSL_RAW_DATA);
  28. return $decrypted;
  29. }
#以前用mcrypt写一个加密大概是这样

public function encryptData($str, $key)
{
$block = mcrypt_get_block_size('des', 'ecb');
$pad = \(block - (strlen(\)str) % $block);
\(str .= str_repeat(chr(\)pad), $pad);
$res = mcrypt_encrypt(MCRYPT_DES, $key, \(str, MCRYPT_MODE_ECB); return strtoupper(bin2hex(\)res));
}

现在你需要用openssl来替换了

public function encryptData($str, $key)
{
\(res = openssl_encrypt(\)str, 'DES-ECB', \(key, OPENSSL_RAW_DATA); return strtoupper(bin2hex(\)res));
}

以前用mcrypt写一个解密大概是这样

public function decryptData($str, $key)
{
\(str = strtolower(\)str);
$str = pack('H*', $str);
$decrypted = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
\(dec_s = strlen(\)decrypted);
\(padding = ord(\)decrypted[$dec_s - 1]);
\(decrypted = substr(\)decrypted, 0, -$padding);
return $decrypted;
}

现在也要用openssl替换了

public function decryptData($str, $key)
{
\(str = strtolower(\)str);
$str = pack('H*', $str);
\(decrypted = openssl_decrypt(\)str,'DES-ECB', $key,OPENSSL_RAW_DATA);
return $decrypted;
}

5、算术运算中警告无效的字符串

  1. # 以前可以这样
  2. \(num = </span><span class="nu0">1</span><span class=""> + </span><span class="st0">'abc323vccc'</span><span class="">; </span><span class="co1"> //result 1;</span><span class=""></span></li><li class=" odd"><span class="">\)num = 1 +'100abc'; //result 101;
  3. #现在你不能那样写了,将会得到这样的一个警告
  4. PHP warning: A non-numeric value encountered on line 1
# 以前可以这样
\(num = 1 + 'abc323vccc'; //result 1; \)num = 1 +'100abc'; //result 101;

现在你不能那样写了,将会得到这样的一个警告

PHP warning: A non-numeric value encountered on line 1

 

						</div>

标签:decrypted,key,...,几个,特性,PHP7.1,str,catch,mcrypt
来源: https://www.cnblogs.com/samRoot/p/14207987.html