找回密码
立即注册
搜索
热搜: Java Python Linux Go
发回帖 发新帖

1431

积分

0

好友

208

主题
发表于 8 小时前 | 查看: 1| 回复: 0

现代PHP早已今非昔比。它经历了漫长的演化,如今已配备JIT编译器、通过readonly关键字实现的不可变性、枚举、属性,以及一个足以媲美TypeScript的健全类型系统。

无需构建步骤。无需转译。无需打包。你只需编写代码并部署即可。这正是它被许多人忽略的卓越生产力。

现代PHP的代码表现力

以下是一些展示其现代语法与强大功能的代码示例:

值对象与模式匹配

final readonly class Book
{
    public function __construct(
        public Status $status,
        public string $title,
    ) {}

    public function label(): string
    {
        return match ($this->status) {
            Status::Draft => 'Working on it',
            Status::Published => 'Ready to read',
        };
    }
}

简洁的数据库查询(使用Laravel的Eloquent ORM

Route::get('/books', function () {
    return Book::query()
        ->where('status', Status::Published)
        ->with('author')
        ->paginate();
});

优雅的测试语法(使用Pest)

it('publishes a book', function () {
    $book = Book::factory()->create();
    $book->publish();

    expect($book->status)->toBe(Status::Published);
});

强类型提示与泛型注解

/**
 * @return array<int, string>
 */
public function titles(): array
{
    return Book::all()
        ->filter(fn (Book $book): bool => $book->isPublished())
        ->map(fn (Book $book): string => $book->title)
        ->toArray();
}

成熟强大的开发生态

如今的PHP不仅仅是变得更好,它与过去的形象已截然不同。整个生态由一系列高效的工具支撑。

Laravel:高效的Web开发框架

通过一行命令即可创建新应用,极大地提升了项目初始化效率。

$ laravel new myapp
   _                               _
  | |                             | |
  | |     __ _ _ __ __ ___   _____| |
  | |    / _` | '__/ _` \ \ / / _ \ |
  | |___| (_| | | | (_| |\ V /  __/ |
  |______\__,_|_|  \__,_| \_/ \___|_|

 Creating a laravel/laravel project...
 ✓ Application ready in [myapp].
 ✓ Built with love.

Composer:卓越的依赖管理

作为PHP的包管理器,Composer 能快速、清晰地管理项目依赖。

$ 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: 1 install, 0 updates, 0 removals
   - Locking laravel/sanctum (v4.0.6)
 ✓ Package laravel/sanctum installed successfully

PHPStan:严格的静态分析

它能在运行前发现代码中的潜在问题,是保障代码质量的重要工具,其检查严格度可调至最高级别。

$ ./vendor/bin/phpstan analyse
 42/42 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
 ───────────────────────────────────────────────────
 [OK] No errors
 💡 Tip: PHPStan is at level 9 — maximum strictness

Pest:愉悦的测试体验

这是一个专注于简洁语法的测试框架,让编写测试成为一种享受。

$ ./vendor/bin/pest
 PEST  v4.0
 ✓ 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:    4 passed (12 assertions)
 Duration: 0.06s

Pint:自动化的代码风格修正

Laravel出品的代码格式化工具,能自动统一代码风格,无需手动调整。

$ ./vendor/bin/pint
 PINT  v1.18
 ✓ app/Models/Book.php                    fixed
 ✓ app/Http/Controllers/BookController.php fixed
 ✓ tests/Feature/BookTest.php             fixed
 3 files fixed

Rector:自动化的代码重构

这个工具可以自动将旧式代码升级为现代语法,大幅降低重构成本。

$ ./vendor/bin/rector --dry-run
 12/12 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
 3 files would have been changed:
   ↳ AddVoidReturnTypeWhereNoReturnRector
   ↳ ReadOnlyClassRector
   ↳ TypedPropertyFromStrictConstructorRector
 [OK] Rector is done! 3 files with changes

结论:PHP还是一个笑话吗?

或许你曾对此深信不疑。但当你深入审视现代PHP及其生态后,会发现它早已脱胎换骨:类型安全、表达力强、开发体验现代化。更重要的是,它能稳定、高效地驱动生产级应用。

立即开始体验

只需一条命令,无需复杂配置,你就能获得一个生产就绪的开发环境。

macOS

安装PHP

/bin/bash -c "$(curl -fsSL https://php.new/install/mac)"

创建并运行Laravel应用

laravel new my-app
cd my-app
composer run dev
# 随后在浏览器中访问 http://localhost:8000

Windows

安装PHP

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

创建并运行Laravel应用

laravel new my-app
cd my-app
composer run dev
# 随后在浏览器中访问 http://localhost:8000

Linux

安装PHP

/bin/bash -c "$(curl -fsSL https://php.new/install/linux)"

创建并运行Laravel应用


laravel new my-app
cd my-app
composer run dev
# 随后在浏览器中访问 http://localhost:8000



上一篇:PyTorch/TensorFlow计算图内存优化:视图变换与策略实践
下一篇:从SAP IBU行业对象模型到Palantir FDE:工程化本体论的企业级实践与演进
您需要登录后才可以回帖 登录 | 立即注册

手机版|小黑屋|网站地图|云栈社区 ( 苏ICP备2022046150号-2 )

GMT+8, 2025-12-24 18:57 , Processed in 0.232562 second(s), 37 queries , Gzip On.

Powered by Discuz! X3.5

© 2025-2025 云栈社区.

快速回复 返回顶部 返回列表