首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >为什么在 2026 年选择 PHP ?

为什么在 2026 年选择 PHP ?

作者头像
Tinywan
发布2026-07-01 12:41:15
发布2026-07-01 12:41:15
780
举报
文章被收录于专栏:开源技术小栈开源技术小栈

为什么在 2026 年选择 PHP?

它现在其实超级棒。PHP 已经走过了很长的路。现代 PHP 拥有 JIT 编译、通过 readonly 实现的不可变性、枚举、属性,以及一个媲美 TypeScript 的类型系统。

无需构建步骤。无需转译。无需打包。只需编写代码并部署。这就是每个人都忘记的超能力。

值对象 API 测试 泛型

代码语言:javascript
复制
final readonly class Book
{
    public function __construct(
        public Status $status,
        public string $title,
    ) {}
    
    public function label(): string
    {
        match ($this->status) {
            Status::Draft => 'Working on it',
            Status::Published => 'Ready to read',
        };
    }
}
代码语言:javascript
复制
Route::get('/books', function () {
    Book::query()
        ->where('status', Status::Published)
        ->with('author')
        ->paginate();
});
代码语言:javascript
复制
it('publishes a book', function () {
    $book = Book::factory()->create();
    $book->publish();
    expect($book->status)->toBe(Status::Published);
});
代码语言:javascript
复制
/**
 * @return array<int, string>
 */
public function titles(): array
{
    Book::all()
        ->filter(fn (Book $book): bool => $book->isPublished())
        ->map(fn (Book $book): string => $book->title)
        ->toArray();
}

生态系统

现代 PHP 不仅仅是更好。它与过去的 PHP 完全不同。

laravel

代码语言:javascript
复制
$ laravel new myapp
   _                               _
  | |                             | |
  | |     __ _ _ __ __ ___   _____| |
  | |    / _` | '__/ _` \ \ / / _ \ |
  | |___| (_| | | | (_| |\ V /  __/ |
  |______\__,_|_|  \__,_| \_/ \___|_|
  Creating a laravel/laravel project...
  ✓ Application ready in [myapp].
  ✓ Built with love.

composer

代码语言:javascript
复制
$ composer require laravel/sanctum
  ./composer.json has been updated
  Running composer update laravel/sanctum
  Loading composer repositories with package information
  Updating dependencies
  Lock file operations: install, updates, removals
    - Locking laravel/sanctum (v4.0.6)
  ✓ Package laravel/sanctum installed successfully

PHPStan

代码语言:javascript
复制
$ ./vendor/bin/phpstan analyse/ [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓]%
  ───────────────────────────────────────────────────
  [OK] No errors
  💡 Tip: PHPStan is at level — maximum strictness

Pest

代码语言:javascript
复制
$ ./vendor/bin/pest
  PEST  v4.
  ✓ it can create a book                     0.02s
  ✓ it can publish a book                    0.01s
  ✓ it validates required fields             0.01s
  ✓ it belongs to an author                  0.02s
  Tests: passed ( assertions)
  Duration: 0.06s

Print

代码语言:javascript
复制
$ ./vendor/bin/pint
  PINT  v1.
  ✓ app/Models/Book.php                    fixed
  ✓ app/Http/Controllers/BookController.php fixed
  ✓ tests/Feature/BookTest.php             fixed files fixed

Rector

代码语言:javascript
复制
$ ./vendor/bin/rector --dry-run/ [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓]% files would have been changed:
  ↳ AddVoidReturnTypeWhereNoReturnRector
  ↳ ReadOnlyClassRector
  ↳ TypedPropertyFromStrictConstructorRector
  [OK] Rector is done! files with changes

仍然认为 PHP 是笑话吗?

我们以前也这么想。然后我们真正去了解了一下。它类型安全。表达力强。现代化。它就是能用。看我解释一下。

今天就开始

一条命令。零配置。生产就绪。

macOS

安装 PHP

代码语言:javascript
复制
/bin/bash -c "$(curl -fsSL https://php.new/install/mac)"

创建 Laravel 应用并运行

代码语言:javascript
复制
laravel new my-app
cd my-app
composer run dev
# 访问 http://localhost:8000

Windows

安装 PHP

代码语言:javascript
复制
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor; iex ((New-Object System.Net.WebClient).DownloadString('https://php.new/install/windows'))

创建 Laravel 应用并运行

代码语言:javascript
复制
laravel new my-app
cd my-app
composer run dev
# 访问 http://localhost:8000

Linux

安装 PHP

代码语言:javascript
复制
/bin/bash -c "$(curl -fsSL https://php.new/install/linux)"

创建 Laravel 应用并运行

代码语言:javascript
复制
laravel new my-app
cd my-app
composer run dev
# 访问 http://localhost:8000
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-12-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 开源技术小栈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 为什么在 2026 年选择 PHP?
  • 值对象 API 测试 泛型
  • 生态系统
    • laravel
    • composer
    • PHPStan
    • Pest
    • Print
    • Rector
  • 仍然认为 PHP 是笑话吗?
  • 今天就开始
    • macOS
    • Windows
    • Linux
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档