PHP Sandbox

PHP.net

References

PHP CLI

# 查看 PHP 编译时的参数
php -r "phpinfo();" | grep configure

# 查看 .ini 配置文件路径
php --ini
php -r "phpinfo();" | grep "Configuration File"

# 查看 Modules
php -m

# 显示 PHP 信息
php -i
php -i | grep -E "Loaded Configuration|Scan this dir"

php -r "echo ini_get('memory_limit');"
php -r "phpinfo();" | grep memory

# 显示扩展配置
php --ri gd

# 检查扩展是否存在
php --re decimal

# 交互式运行模式。具有函数、常量、类名、变量、静态方法调用和类常量的 `tab` 补全功能
# http://php.net/manual/en/features.commandline.interactive.php
php -a

# 检查 PHP-FPM 配置文件语法
php-fpm -tt

Snippets

// 临时设置最大内存占用
ini_set('memory_limit', '1024M');
// 设置脚本最大执行时间为 0 永不过期
set_time_limit(0);

Composer

aliyun repo

阿里云 Composer 全量镜像

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
    "config": {
        "disable-tls": true,
        "gitlab-domains": [],
        "optimize-autoloader": true,
        "preferred-install": {
            "*": "dist"
        }
        "secure-http": false,
        "sort-packages": true,
    },
    "repositories": [
        {
            "type": "cvs",
            "url": "..."
        },
        {
            "type": "composer",
            "url": "https://mirrors.tencent.com/composer/"
        },
        {
            "type": "composer",
            "url": "https://mirrors.aliyun.com/composer/"
        },
        {
            "type": "composer",
            "url": "https://asset-packagist.org"
        }
    ]

忽略 php 版本限制

这个是错误做法,这样会造成库安装的版本错误。不应该使用。

composer require hellogerard/jobby --ignore-platform-reqs

推荐做法:

which composer
# /usr/local/bin/composer

# {正确的 PHP 版本}/bin/php /usr/local/bin/composer require hellogerard/jobby
# 如果内存不够,可以设置为不限制
/usr/local/opt/php@7.1/bin/php -d memory_limit=-1 /usr/local/bin/composer update -vvv

# 若项目之前已通过其他源安装,则需要更新 composer.lock 文件
composer update --lock

PHPStan

class Foo {
   public const WHEELER = [
      'car' => 4,
      'bike' => 2,
   ];
}
/**
 * @param key-of<Foo::WHEELER> $type
 * @param value-of<Foo::WHEELER> $wheels
 */
function repair(string $type, int $wheels): void
{
    // $type is 'bike'|'car'
    // $wheels is 2|4
}
repair("bus", 2);
// Parameter #1 $type of function repair expects 'bike'|'car', 'bus' given.

– EOF –