惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
Vercel News
Vercel News
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
小众软件
小众软件
美团技术团队
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿
D
Docker
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
博客园 - 聂微东
Blog — PlanetScale
Blog — PlanetScale
H
Hackread – Cybersecurity News, Data Breaches, AI and More
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
B
Blog
雷峰网
雷峰网
The Cloudflare Blog

mafeifan 的编程技术分享

mafengwo-mp3-downloader | mafeifan 的编程技术分享 示例页面 | mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 查看 default namespace 下的 default service account名称 mafeifan 的编程技术分享 检查日志 | mafeifan 的编程技术分享 bridge fdb show dev flannel.1 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享
mafeifan 的编程技术分享
2026-01-16 · via mafeifan 的编程技术分享
  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. 匿名类

php

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

<?php
$b = array();
each($b);

// Deprecated:  The each() function is deprecated. This message will be suppressed on further calls

兼容方法

php

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

兼容方法

php

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-runninghttp://php.net/manual/zh/language.oop5.anonymous.phphttps://www.cnblogs.com/phpnew/p/7991572.html