PHP 8.0兼容处理

PHP 8.0: ReflectionParameter::getClass())

在 php8.0中使用此方法会报弃用的错误,需要改成

// php8之前写法
$className = $reflectionParam->getClass();
// php8之后写法
$className = $reflectionParam->getType() && !$reflectionParam->getType()->isBuiltin() 
   ? new ReflectionClass($reflectionParam->getType()->getName())
   : null;

php 安装xhprof

当前环境 ubuntu20.04 php7.4.3

用pecl install xhprof

安装完成后修改php.ini 添加扩展和设置日志输出

## 添加扩展
extension=xhprof.so
## 设置输出路径
xhprof.output_dir=/home/asd/xhprof/log

完成后重启php

在入口文件增加代码

xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);

注册php在终止执行时的函数

register_shutdown_function(function(){
	$data = xhprof_disable();   //返回运行数据
	//xhprof_lib 在下载的包里存在这个目录,记得将目录包含到运行的php代码中
	include '/usr/share/php/xhprof_lib/utils/xhprof_lib.php';
	include '/usr/share/php/xhprof_lib/utils/xhprof_runs.php';
	$objXhprofRun = new \XHProfRuns_Default();
	$objXhprofRun->save_run($data, "test"); //test 表示文件后缀
});

运行。发现/home/asd/xhprof/log下有日志文件

查看关系图

将/usr/share/php/xhprof_html 设置成可以通过nginx访问,访问index.php文件可以查看图

查看图之前需要安装 sudo apt install graphviz

xdebug3配置

xdebug2配置

zend_extension=”xdebug.so”

xdebug.remote_enable=on

xdebug.remote_port=9001

xdebug.remote_host=127.0.0.1

在升级3.0版本后,remote_enable 被mode 取代,remote_host被 client_host 取代,remote_port改为 client_port,默认为9003端口,client_host默认为 localhost,所以3之后的配置为

zend_extension=”xdebug.so”
xdebug.mode=debug
xdebug.client_host=127.0.0.1

我本地是9003端口,所以此处省略端口配置

PHP 毫秒时间戳

大多数网上都是这种

function msectime()
{
    list($msec, $sec) = explode(' ', microtime());
    $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    return $msectime;
}

但是,PHP `microtime`函数可以支持毫秒

`round(microtime(true)*1000)` 可以输出毫秒时间戳

PHP如何读取大文件

这里使用的yield 方法

function readTheFile($path) {
    $handle = fopen($path, 'r');

    while(!feof($handle)) {
        yield trim(fgets($handle));
    }

    fclose($handle);
}

$iterator = readTheFile('shakespeare.txt');
foreach ($iterator as $item) {
    echo $item;
}

php xdebug配置

zend_extension=xdebug.so
xdebug.remote_enable=on
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.cli_color = 2

php编写swagger注释信息

  1. 需要写title,不用每个文件都写,只要有一个就行。
/**
 * @OA\Info(
 *     title="My Test API",
 *     version="1.0.0"
 * )
 */

2. 写注释信息

/**
	 * @OA\Post(
	 *     path="路径,比如:/test/push",
	 *     tags={"标签"},
	 *     summary="描述信息",
	 *     @OA\RequestBody(
	 *         @OA\MediaType(
	 *             mediaType="application/json  请求格式",
	 *             @OA\Schema(
         *                 required={"id"},
	 *                 @OA\Property(
	 *                     property="id",
	 *                     type="int",
	 *                     description="id 参数"
	 *                 ),
	 *                 @OA\Property(
	 *                     property="sess",
	 *                     type="string",
	 *                     description="session 参数"
	 *                 ),
	 *                 example={"id": 1, "sess": "wdff"}
	 *             )
	 *         )
	 *     ),
	 *     @OA\Response(
	 *         response=200,
	 *         description="successful",
	 *         @OA\JsonContent(
	 *          type="array|object|string",
	 *          @OA\Items(
	 *          	@OA\Property(property="seminarName", type="string", description="主会名"),
	 *          	@OA\Property(property="seminarId", type="int", description="主会id"),
	 *          	@OA\Property(
	 *              	property="items",
	 *              	type="object",
	 *                  	@OA\Property(property="subSeminarId", type="int", description="分会id"),
	 *                  	@OA\Property(property="subName", type="string", description="分会名")
	 *              	
	 *          	)
	 * 			)
	 *         )
	 *
	 *     )
	 * )
	 */

3. 生成swagger文件

# 首选需要composer require
composer require zircote/swagger-php
# 命令
vendor/bin/openapi -o openapi.json test/
# -o 输出后的文件
# test/  目录名,swagger会从此目录scan