PHP

PHP 8.2 新特性

PHP 8.2 将于2022 年 12 月 8 日发布。在本文中,我们将逐一介绍所有新特性、性能改进、更改和弃用。

只读类RFC

PHP 8.1 中引入了只读属性。此 RFC 构建在它们之上,并添加了语法糖以使所有类属性同时变为只读。代替这样写:

class Post
{
    public function __construct(
        public readonly string $title, 
        public readonly Author $author,
        public readonly string $body
            

Vite 现在是 Laravel 应用程序的默认前端资产捆绑器

Laravel 团队一直在努力为 Laravel 集成Vite 。截至本周,Vite 现在是新 Laravel 项目中的默认前端资产捆绑器,同时更新了 Breeze 和 Jetstream:

    

PHP升级到8.0后,把Fatal error: Array and string offset access syntax with curly braces is no longer supported in解决

PHP升级到8.0后,把Fatal error: Array and string offset access syntax with curly braces is no longer supported in解决

 

PHP8.0不再能够使用花括号来访问数组或者字符串的偏移.需要将{}修改成[] 就可以解决问题

 

若代码逻辑中含有类似

$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
需要修改成

$asc = ord($s[0]) * 256 + ord($s[1]) - 65536;…

PHP升级到8.0后,报Fatal error: Uncaught Error: Call to undefined function create_function()解决方案.

PHP升级到8.0后,报Fatal error: Uncaught Error: Call to undefined function create_function()解决方案.

因为php8.0 已经把create_function移除了.所以有2种解决 方法 .

一.是把create_function改为匿名函数.如下所示

<?php
$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo $newfunc(2, M_E) . "\n";
?>

改为

<?php
$newfunc = function($a,$b) { 

Laravel 的 ORM 缓存包

LaraCache是一个基于 ORM 的 Laravel 包,用于基于模型查询创建、更新和管理缓存项。使用此包,您可以缓存在整个应用程序中大量使用的查询。

use Mostafaznv\LaraCache\Traits\LaraCache;
 
class Article extends Model
{
    use LaraCache;
 
    public static function cacheEntities(): array
    {
        return [
            CacheEntity::make('list.forever')
                ->cache(function() {
                    return Article::query()->latest()->get();
                }),
 
            CacheEntity::make('latest')
                ->validForRestOfDay()
                ->cache(function() {
                    return Article::query()->latest()->first();
                })
        ];
    }
}

使用该

                

PHP监控一个进程是否在运行的方法

PHP监控一个进程是否在运行的方法

 

exec("pgrep lighttpd", $pids);
if(empty($pids)) {

    // lighttpd is not running!
}

pgrep -f  全名匹配.

 

$mabbitmq=shell_exec("pgrep [r]abbitmq"); //https://stackoverflow.com/questions/51551908/why-is-the-result-equal-to-0-and-sometimes-1-shell-execpgrep-f
$mabbitmq = str_replace(PHP_EOL, '', $mabbitmq);
$mongodb=shell_exec("pgrep [m]ongo");
$mongodb = str_replace(PHP_EOL, '', $mongodb);
$ydmcuclient=shell_exec("pgrep -f [y]dmcu_client");
$ydmcuclient
        

原生php递归删除文件+文件夹

原生php递归删除文件+文件夹

<?php
function deldir($dirname){
  if(file_exists($dirname)) {//判断是否存在文件夹
    $dir=opendir($dirname);//打开文件夹
    while($filename=readdir($dir)){//读取123文件夹里面的数据
      if($filename!="." && $filename!=".."){//除去.并且..除外
        $file=$dirname."/".$filename;
          if(is_dir($file)){
            deldir($file); //使用递归删除子目录,就是说判断123文件夹里面有没有文件夹,如果有遍历然后删除
          }else{
            echo '删除文件<b>'.$file.'</b>成功<br>';
            unlink($file);
          }
      }
  }
  closedir($dir);
  echo '删除目录<b>'.$dirname.'</b>成功<br>';
  rmdir($dirname);
  }
}
deldir('D:3');//比如文件在d盘的123目录下
?>