PHP Code Snippet

2023-08-23 18:38:05 浏览数 (1)

PHP Sandbox

  • PHP Sandbox
  • https://legacy-sandbox.onlinephpfunctions.com/

PHP.net

  • Supported Versions | PHP.net
  • Unsupported Branches | PHP.net

References

  • PHP News, Articles, Upcoming Changes, and more | PHP.Watch

Laravel illuminate

代码语言:javascript复制
# https://github.com/illuminate/database/blob/master/README.md
$capsule = new Capsule();
$capsule->addConnection($this->dbConfig);
$capsule->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();
$capsule->getDatabaseManager()->extend('mongodb', function ($config, $name) {
    $config['name'] = $name;

    return new JenssegersMongodbConnection($config);
});

参数查看

代码语言:javascript复制
# 查看 PHP 编译时的参数
php -r "phpinfo();" | grep configure

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

# 查看 Modules
php -m

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

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

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

查看、修改内存限制

代码语言:javascript复制
php -r "echo ini_get('memory_limit');"

php -r "phpinfo();" | grep memory
代码语言:javascript复制
memory_limit = 1024M;
代码语言:javascript复制
// 临时设置最大内存占用
ini_set('memory_limit', '1024M');
// 设置脚本最大执行时间为 0 永不过期
set_time_limit(0);

Too Many Open Files

PHP-FPM Too Many Open Files 24 Error (set open file descriptor limit)

代码语言:javascript复制
vim php-fpm.conf

;rlimit_files = 1024
rlimit_files = 4096

重启 PHP-FPM。

Composer

aliyun repo

阿里云 Composer 全量镜像

代码语言:javascript复制
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
代码语言:javascript复制
"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 版本限制

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

代码语言:javascript复制
composer require hellogerard/jobby --ignore-platform-reqs

推荐做法:

代码语言:javascript复制
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

emory-limit-errors for more info on how to handle out of memory errors

代码语言:javascript复制
php -d memory_limit=-1 /usr/local/bin/composer update

更新 composer.lock

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

代码语言:javascript复制
composer update --lock

homebrew PHP

0 人点赞