/ code-snippet  

PHP Code Snippet

PHP Sandbox

PHP.net

References

Laravel illuminate

# 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 \Jenssegers\Mongodb\Connection($config);
});

参数查看

# 查看 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

查看、修改内存限制

php -r "echo ini_get('memory_limit');"

php -r "phpinfo();" | grep memory
memory_limit = 1024M;
// 临时设置最大内存占用
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)

vim php-fpm.conf

;rlimit_files = 1024
rlimit_files = 4096

重启 PHP-FPM。

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

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

php -d memory_limit=-1 /usr/local/bin/composer update

更新 composer.lock

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

composer update --lock

homebrew PHP

🍺 Homebrew tap for PHP 5.6 to 8.2. PHP 8.2 is a nightly build.

shivammathur/homebrew-php

brew tap shivammathur/php

brew install shivammathur/php/php@8.1 -vvv

brew link --overwrite --force shivammathur/php/php@8.1
php -v

旧版本 MacOS 可以安装报错时尝试删除 XCode。

brew untap shivammathur/php

Real Client IP

# PHP7+
$clientIP = $_SERVER['HTTP_CLIENT_IP']
?? $_SERVER["HTTP_CF_CONNECTING_IP"] # when behind cloudflare
?? $_SERVER['HTTP_X_FORWARDED']
?? $_SERVER['HTTP_X_FORWARDED_FOR']
?? $_SERVER['HTTP_FORWARDED']
?? $_SERVER['HTTP_FORWARDED_FOR']
?? $_SERVER['REMOTE_ADDR']
?? '0.0.0.0';

动态实例化类

class Test1{
public function __construct(){
echo "Test1<br>";
}
}

// 方法一
$class1 = "Test1";
new $class1();

// 方法二
$class2 = "Test2";
// 建立类的反射
$class2 = new ReflectionClass($class2);
// 相当于实例化类
$instance = $class2->newInstance();

– EOF –