PHP 8.5 · ThinkPHP 3→8 迁移 · Web 全栈速查手册

高频开发语法、老版本 ThinkPHP 升级对照表、MySQL 索引口诀与 Linux/Nginx 常用命令一页速查。

ThinkPHP 迁移

ThinkPHP 3.0/3.2 → ThinkPHP 8 核心函数对照表

ThinkPHP 3.x 经典写法ThinkPHP 8 现代写法说明
M('User')->where($map)->select()Db::name('user')->where($map)->select()表名自动加前缀,返回数据集对象
D('User')->find($id)UserModel::find($id)强类型模型静态调用
I('post.name', '', 'trim')request()->post('name/s', '', 'trim')强制类型修饰符 /s /d /a
U('Article/detail', ['id'=>5])(string) url('article/detail', ['id'=>5])路由生成需转字符串或 ->build()
C('DB_HOST')config('database.connections.mysql.hostname')点语法读取多级配置
S('key', $val, 3600)cache('key', $val, 3600)PSR-16 统一缓存接口
$this->ajaxReturn($data)return json($data);响应对象由控制器直接 return
PHP 8.4 / 8.5

PHP 8.4 & 8.5 现代语法精选

// 1. PHP 8.4 属性钩子 (Property Hooks)
class User {
    public string $email {
        set => strtolower(trim($value));
    }
}

// 2. PHP 8.4 不对称可见性(公开读、私有写)
class Order {
    public private(set) string $orderNo;
}

// 3. PHP 8.4 无需括号的 new 链式调用
$slug = new Slugger()->generate('PHP 8.5 教程');

// 4. PHP 8.5 管道操作符 (Pipe Operator |>)
$clean = $rawInput
    |> trim(...)
    |> strip_tags(...)
    |> mb_strtolower(...);

// 5. PHP 8.5 #[NoDiscard] 防止忽略返回值
#[\NoDiscard('必须检查执行状态')]
function saveOrder(array $data): bool { /* ... */ }
PHP 安全

PHP + PDO 安全编程铁律

// 禁用模拟预处理,彻底杜绝宽字节与二阶注入
$pdo = new PDO($dsn, $user, $pass, [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES   => false,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);

// 强制参数绑定
$stmt = $pdo->prepare('SELECT id, title FROM articles WHERE slug = ? LIMIT 1');
$stmt->execute([$slug]);

// 密码哈希:告别 md5/sha1
$hash = password_hash($plainPwd, PASSWORD_DEFAULT);
$ok   = password_verify($inputPwd, $hash);

// HTML 模板输出防 XSS
function e($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
AI × PHP

PHP SSE 流式推送大模型响应模板

header('Content-Type: text/event-stream; charset=UTF-8');
header('Cache-Control: no-cache');
header('X-Accel-Buffering: no'); // 关闭 Nginx 缓冲

$ch = curl_init('https://api.example.com/v1/chat/completions');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'model' => 'deepseek-chat',
        'stream' => true,
        'messages' => $messages,
    ]),
    CURLOPT_WRITEFUNCTION => function ($ch, $chunk) {
        echo $chunk;
        if (ob_get_level() > 0) ob_flush();
        flush();
        return strlen($chunk);
    },
]);
curl_exec($ch);
curl_close($ch);
MySQL 调优

MySQL 索引与 EXPLAIN 诊断速查

EXPLAIN type性能等级典型排查建议
const / eq_ref最优主键或唯一索引精确命中
ref / range良好普通索引等值或范围扫描(IN/BETWEEN/>)
index一般扫描整棵索引树,检查是否可加过滤条件
ALL需优化全表扫描!检查索引缺失、隐式类型转换或函数运算

深分页延迟关联写法:先用覆盖索引定位主键,再回表 INNER JOIN:

SELECT a.* FROM articles a
INNER JOIN (
    SELECT id FROM articles WHERE category_id = 2
    ORDER BY published_at DESC LIMIT 100000, 10
) t ON a.id = t.id;
DIV + CSS

现代 CSS Flexbox / Grid 高频布局片段

/* 1. 自适应多列卡片(无需繁琐媒体查询) */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 20px;
}

/* 2. 经典双栏:主内容 + 固定宽度侧边栏 */
.main-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 28px;
}

/* 3. 移动端防撑破宽屏核心三件套 */
*, *::before, *::after { box-sizing: border-box; }
img, pre, table { max-width: 100%; }
pre, .table-wrap { overflow-x: auto; }
jQuery & JS

jQuery 实战技巧与原生 ES6+ 对应写法

场景jQuery 写法现代原生 JS 写法
事件委托$('#list').on('click', '.item', fn)list.addEventListener('click', e => { const el = e.target.closest('.item'); })
异步请求$.ajax({url, method:'POST', data})await fetch(url, {method:'POST', body: JSON.stringify(data)})
切换类名$el.toggleClass('active')el.classList.toggle('active')
文档就绪$(function(){ ... })document.addEventListener('DOMContentLoaded', ...)
Linux 运维

Linux + Nginx + PHP-FPM 生产排障常用命令

# 检查 Nginx 配置语法并平滑重载
nginx -t && nginx -s reload

# 快速排查 502 / PHP-FPM 慢请求
tail -f /www/wwwlogs/zoneself.org.error.log
ps aux | grep php-fpm | wc -l

# 查看磁盘与内存占用 Top 进程
df -h && free -m
ps aux --sort=-%mem | head -n 10

# 排查端口监听状态
ss -tulpn | grep -E ':80|:443|:3306'