博文

目前显示的是 二月, 2021的博文

PHP8 —— New String Helpers

新增了三个字符串函数,str_starts_with, str_ends_with, str_contains, PHP 的函数这么方便,很难想象竟然一直没有这几个。 str_starts_with 判断字符串是否以另一个字符串开头,在PHP7以及之前 $id = 'inv_abcdefgh'; $result = strpos($id, 'inv_') === 0; var_dump($result); // true PHP8 中可以直接这么写 $result = str_starts_with($id, 'inv_'); str_ends_with 判断字符串是否以另外一个字符串结尾,在 PHP7 及之前,比较麻烦,通常是这么写 $id = 'abcd_inv'; $result = strpos(strrev($id), strrev('_inv')) === 0; 或者 $result = substr($id, -1 * strlen('_inv')) === '_inv'; 或者上正则吧 $result = preg_match('/_inv$/', $id) === 1; 看起来都是比较麻烦的。PHP8 里面可以简化成下面这样了 $id = 'abcd_inv'; $result = str_ends_with($id, '_ind'); str_contains 字符串包含,PHP8 之前一般就是 strpos 来实现了 $url = 'https://example?for=bar'; $result = strpos($url, '?') !== FALSE; PHP8 就直接一点 $result = str_contains($url, '?');

PHP8 —— Match Expressions

假设我们要保存发生的事件,根据时间的 class 来获取入库时的名称,PHP8 之前可以用 switch 实现 class Conversation {} $obj = new Conversation(); switch (get_class($obj)) { case 'Conversation': $type = 'started_conversation'; break; case 'Replay': $type = 'replied_to_conversation'; break; case 'Comment': $type = 'commented_on_lesson'; break; } echo $type; 显然这样写起来很繁琐。PHP8之后,可以使用 match expression 来改写,用键值对直接表达对应关系,省略了 case 、break等关键字。 $type = match(get_class($obj)) { 'Conversation' => 'started_conversation', 'Replay' => 'replied_to_conversation', 'Comment' => 'commented_on_lesson' } 当然,除了直接返回值之外,也能返回表达式;对相同返回的分支也可以做合并。比如使用match来实现一个请求路由。 $status = match($request_method) { 'post' => $this->handlePost(), 'get', 'head' => $this->handleGet(), default => throw new \Exception

PHP 8 —— Nullsafe Operator

<?php class User { public function profile() { return getPorfileFromDb(); //数据库查询 } } class Profile { public function employment() { return 'web developer'; } } $user = new User(); echo $user->profile()->employment(); 如果profile() 返回了 null, 那么会报 Call a member function on null 。 PHP8之前,我们需要加个检查。 $profile = $user->profile(); if ($profile) { echo $profile->employment(); } PHP 8 提供了Nullsafe Operator,直接写成下面这种形式 $user->profile()?->employment(); 如果 $user->profile()为空,则上面的表达式最终值为 NULL。 另外这种写法是可以串起来的,像下面这样 $user?->profile()?->employment(); 也可以结合 ?? 写成下面这种形式 echo $user->profile()?->employment() ?? 'Not provided';

Vue3新特性(1) —— Vite

图片
Vite (法语 fast )是 Vue3 的默认构建工具,在开发环境下基于浏览器原生 ES module 开发,从而实现按需编译,在生产环境下基于 Rollup 打包。它具有如下几个特点: 快速的冷启动 即时的热模块更新 真正的按需编译 开始使用 终端执行 $ npm init vite-app vite-example 生成一个 vite 项目,结构如下 ├── index.html ├── package.json ├── public │   └── favicon.ico └── src ├── App.vue ├── assets │   └── logo.png ├── components │   └── HelloWorld.vue ├── index.css └── main.js 进入项目目录,执行 $ cd vite-example $ npm install && npm run dev 可以看到dev server启动速度非常快,大概2s不到 vite 特性 打开chrome的开发工具,看到浏览器直接加载了 vue 文件,后面跟了参数 type 看下源代码的 main.js import { createApp } from 'vue' import App from './App.vue' import './index.css' createApp(App).mount('#app') 对比一下浏览器返回的 main.js import { createApp } from '/@modules/vue.js' import App from '/src/App.vue' import '/src/index.css?import' createApp(App).mount('#app') 对vue引用转化为/@modules/vue.js 对./App.vue转换为/src/App.vue 对./index.css转化为/src/index.css