php类备忘录

public(公开):外部、内部、子类全通车
protected(受保护):家族内可用
private(私有):仅当前类可用
 
基础类class
abstract 抽象类,做基础架构、只能继承不能修改,extends后才能new实例化,做model之类的定义
final 最终完美类, 不能extends,可以new实例化
trait 代码复用,不能new初始化,只能class use引入
interface 接口类,不能被 new实例化,使用 implements 来实现接口, 一个类只能继承(extends)一个父类,但可以同时实现(implements)多个接口。

int 整数类型
string 字符串
float 浮点型 带小数点的
bool 布尔型 true或false
array 数组
object 对象
callable 可调用 只能是一个函数、闭包或者方法名
void 空
mixed 混合,任何类型(字符串、数字、数组、对象、甚至 null)
(int|string $id) 联合类型

function 函数
return 直接返回

vscode 配置备忘录

VScode扩展插件安装pretty-php

apt install php-cli php-mbstring -y  
apt install tilix -y
#macos配置文件路径
/Users/用户名Library/Application\ Support/Code/User/settings.json

#Linux配置文件目录
/home/用户名/.config/Code/User/settings.json
{
    "workbench.colorTheme": "Default Light Modern",
    "editor.accessibilitySupport": "off",
    "editor.formatOnType": true,
    // ==== PHP ====
    "[php]": {
        "editor.defaultFormatter": "lkrms.pretty-php",
        "editor.formatOnSave": true
    },

    // ==== HTML ====
    "[html]": {
        "editor.defaultFormatter": "vscode.html-language-features",
        "editor.formatOnSave": true
    },
    // ==== TPL / 模板 ====
    "files.associations": {
        "*.tpl": "html",
        "*.tpl.php": "html"
    },
    "html.format.indentInnerHtml": true,
    "html.format.wrapLineLength": 600,
    "html.format.templating": true,
    "html.format.contentUnformatted": "script,style,pre,code,template,meta",

    // ==== 编辑器外观 ====
    "editor.fontFamily": "Menlo, Monaco, 'Courier New', JetBrains Mono, monospace",
    "editor.fontSize": 14,
    "continue.showInlineTip": false,
    "editor.tokenColorCustomizations": {},
    "editor.defaultFormatter": "lkrms.pretty-php",
    "editor.formatOnPaste": true,
    "css.format.braceStyle": "expand",
    "amazonQ.allowFeatureDevelopmentToRunCodeAndTests": {
        
    }
}

thinkphp在model里面切换分表

最近开始玩各种上TB的数据库,为了提升性能使用了分表
<?php
namespace app\model;
use think\Model;
class Blog extends Model
{
    protected $name = 'blog;
    protected $suffix = '0000';
    public static int $blog_id = 1;

    public static function shard(?int $id = null)
    {
        if (is_null($id)) {
            $id = self::$blog_id ;
        }
        $table = sprintf('%04d', intdiv($id, 10000));
        return self::suffix($table);
    }
}

读取数据

$blog = Blog::suffix('0001')->find(1);
$blog->name = 'test';
$blog->save();
print_r($blog);

 简化一下

Blog::$blog_id =100000;
Blog::shard()->find(1);

 

批量更新必须初始化才能使用

$data = new Blog;
$data->setSuffix('0002')->saveAll($updateList);

目前基本实现了在blog_0000 blog_0001 blog_0002 等各种分表里面读写数据

感觉thinkphp的model DB切换不是很灵活。

 

还有一些坑,翻烂了doc文档库都没找到,待更新。

php使用browscap判断浏览器客户端类型

官方项目地址:https://github.com/browscap/browscap

官方网站:https://browscap.org/

下载配置

wget "https://browscap.org/stream?q=PHP_BrowsCapINI' -O  /etc/php/browscap.ini

php.ini里面加入

browscap = /etc/php/browscap.ini

 

测试代码

        $browser = get_browser(null, true);
        print_r($browser);

测试结果

Array
(
    [browser_name_regex] => ~^mozilla/5\.0 \(.*linux.*android.4\.2.*\).*applewebkit.*\(.*khtml.*like.*gecko.*\).*version/.*safari.*$~
    [browser_name_pattern] => Mozilla/5.0 (*Linux*Android?4.2*)*applewebkit*(*khtml*like*gecko*)*Version/*Safari*
    [parent] => Android Browser Generic
    [comment] => Android Browser Generic
    [browser] => Android
    [browser_maker] => Google Inc
    [platform] => Android
    [ismobiledevice] => 1
    [device_type] => Mobile Phone
    [device_pointing_method] => touchscreen
    [version] => 0.0
    [majorver] => 0
    [minorver] => 0
    [istablet] =>
    [crawler] =>
)

php简繁转换安装部署

yum install epel-release -y
yum install  doxygen -y
git clone https://github.com/BYVoid/OpenCC.git
cd OpenCC
make
sudo make install

git clone https://github.com/nauxliu/opencc4php
cd opencc4php
phpize
./configure
 make && sudo make install
echo  extension=opencc.so >> /opt/php7/etc/php.d/1-opencc.ini

 

 composer载入php-opencc

 composer require overtrue/php-opencc -vvv

 

 

 

 

 

 

cloudpanel安装swoole扩展

apt update
apt install -y build-essential autoconf automake pkg-config libtool cmake clang -y
apt install -y php8.1-dev php8.4-cli php8.4-common -y
apt install -y libssl-dev libcurl4-openssl-dev libpq-dev libsqlite3-dev libzstd-dev libbrotli-dev liburing-dev libssl-dev -y

 git clone https://github.com/swoole/swoole-src.git &&    cd swoole-src
/usr/bin/phpize8.4
./configure \
   --with-php-config=/usr/bin/php-config8.4 \
  --enable-swoole \
  --enable-sockets \
  --enable-mysqlnd \
  --enable-swoole-curl \
  --enable-swoole-pgsql \
  --enable-swoole-sqlite \
  --enable-swoole-stdext \
  --enable-zstd \
  --enable-brotli \
  --enable-uring-socket 

make && make install

echo extension=swoole.so >/etc/php/8.4/cli/conf.d/20-swoole.ini

 

systemctl服务

[Unit]
Description=Swoole HTTP Server
After=network.target
StartLimitIntervalSec=0

[Service]
Type=simple

ExecStart=/usr/bin/php /opt/start.php start swoole
ExecReload=/bin/kill -USR1 $MAINPID

# 自动重启策略
Restart=always
RestartSec=3

# 防止僵尸进程
KillMode=process
KillSignal=SIGTERM
TimeoutStopSec=30

# 资源限制
LimitNOFILE=1048576
LimitNPROC=65535

# 日志
StandardOutput=journal
StandardError=journal

User=www-data
Group=www-data

[Install]
WantedBy=multi-user.target

centos/AlmaLinux/RockLinux 9安装php缺libzip oniguruma扩展包解决办法

最近安装php发现编译失败, 搜索一圈后发现软件包仓库改crb去了,稍微改一下脚本判断就解决了.

sed -i  's/enabled=0/enabled=1/g' /etc/yum.repos.d/*-{crb,plus}.repo

 如果不想改repo配置,则安装的时候启用一下crb源.

 dnf  -y --enablerepo=crb install  libzip-devel oniguruma-devel rrdtool-devel

https://mirror.stream.centos.org/9-stream/CRB/x86_64/os/Packages/

 

另外libc-client-devel的包暂时也没找到, 用remi的源安装uw-imap-devel替代解决了.

dnf install -y http://rpms.remirepo.net/enterprise/remi-release-9.rpm
dnf -y --enablerepo=remi install uw-imap-devel

php扩展mailparse安装

git clone https://github.com/php/pecl-mail-mailparse.git mailparse
cd mailparse
phpize
./configure
sed -i 's/#if\s!HAVE_MBSTRING/#ifndef MBFL_MBFILTER_H/' ./mailparse.c
make
make install 
 echo "extension=mailparse.so" | sudo tee   $(php-config --ini-dir)/mailparse.ini

开源项目地址 https://github.com/php-mime-mail-parser/php-mime-mail-parser

files.photo.gallery

演示地址:https://files.photo.gallery/demo/?samples

下载链接:https://cdn.jsdelivr.net/npm/files.photo.gallery/index.php

设置说明:https://forum.photo.gallery/viewtopic.php?f=66&t=9964

开心补丁:https://cdn.jsdelivr.net/gh/yyingc/[email protected]/files.js


php组建需求: fileinfo exif imagemagick


配置根目录、密码访问和排除不需要显示的目录:

// 根目录配置
'root' => '根目录路径', // root path relative to script.
'start_path' => false, // start path relative to script. If empty, root is start path
// 登录账号密码配置
'username' => 'zhujizixun',
'password' => '12345678', // Add password directly or use https://tinyfilemanager.github.io/docs/pwd.html to encrypt the password (encrypted password is more secure, as it prevents your password from being exposed directly in a file).
// 排除文件或者目录
'files_exclude' => '/.(html|xml)$/i', // '/.(pdf|jpe?g)$/i'
'dirs_exclude' => '//js|/_files(/|$)/i', //'//Convert|/football|/node_modules(/|$)/i',
'allow_symlinks' => true, // allow symlinks