PHP 8.1 Enum (enumeration) Get enumeration by name Author: Chuwen Time: 2023-09-21 Classification: PHP comment Inspired by: https://github.com/php/php-src/issues/9352#issuecomment -1216839118 ```php <? php enum Gender { case UNKNOWN; case MALE; case FEMALE; } /** * @template T as BackedEnum|UnitEnum * @param class-string<T> $enumClass * @param string $name * @return T|null */ function tryFromName(string $enumClass, string $name) { $constName = $enumClass . '::' . $ name; return is_subclass_of($enumClass, UnitEnum::class) && defined($constName) ? constant($constName) : null; } // enum(Gender::UNKNOWN) var_dump(tryFromName(Gender::class, 'UNKNOWN')); // enum(Gender::MALE) var_dump(tryFromName(Gender::class, 'MALE')); // enum(Gender::FEMALE) var_dump(tryFromName(Gender::class, 'FEMALE')); // NULL var_dump(tryFromName(Gender::class, 'null')); ```
Php cs fix only formats the modified code/only formats the submitted code Author: Chuwen Time: 2023-09-21 Classification: PHP comment ##Premises Note that you need to add 'cs fix': "php cs fix $1" 'in the' scripts: 'section of' composer. json ', as shown in the following example ```json { "name": "hyperf/hyperf-skeleton", "type": "project", // ... "autoload": { "psr-4": { "App\\": "app/" }, "files": [] }, "scripts": { // ...... "cs-fix": "php-cs-fixer fix $1", // ...... }, } ``` ##Command ###Only format the modified code ```shell git diff --name-only --cached | grep '\.php$' | xargs -n1 composer cs-fix ``` 1. 'git diff -- name only -- cached': This part of the command is used to obtain the list of modified files that you have staged. 2. 'grep' . Php $'': pipe the file list to the grep command, which will only select files with the extension '. php'. 3. 'xargs - n1 composer cs fix': The xargs command passes each file name as a parameter to the 'composer cs fix' command to format the code of each file. ###Format a submitted code ```shell git diff-tree --no-commit-id --name-only -r <commit-hash> | grep '\.php$' | xargs -n1 composer cs-fix ``` 1. 'git diff tree -- no commit id -- name only - r<commit hash>': This command obtains the modified file list in a specific submission record. >* *<commit hash>* * Can be obtained from ` git log ' 2. 'grep' . Php $'': pipe the file list to the 'grep' command, which will select only files with the extension '. php'. 3. The 'xargs - n1 composer cs fix': 'xargs' command passes each file name as a parameter to the' composer cs fix 'command to format the code of each file.
Pecl install spool installation error: fatal error: pcre2. h: No such file or directory Author: Chuwen Time: 2023-09-19 Classification: PHP , Other classifications comment Installation error log: ```log In file included from /home/linuxbrew/.linuxbrew/Cellar/ php@8.1 /8.1.23/include/php/ext/spl/spl_iterators.h:22, from /home/linuxbrew/.linuxbrew/Cellar/ php@8.1 /8.1.23/include/php/ext/spl/spl_array.h:22, from /tmp/pear/temp/swoole/ext-src/swoole_coroutine.cc:27: /home/linuxbrew/.linuxbrew/Cellar/ php@8.1 /8.1.23/include/php/ext/pcre/php_pcre.h:23:10: fatal error: pcre2.h: No such file or directory 23 | #include "pcre2.h" | ^~~~~~~~~ compilation terminated. make: *** [Makefile:233: ext-src/swoole_coroutine.lo] Error 1 ERROR: `make' failed ``` ##Error reason This error message indicates that the pcre2. h header file is missing in the compilation or construction process, so it cannot be found. Pcre2. h is the header file of PCRE2 (Perl Compatible Regular Expressions) library, which is usually used for regular expression operations. ##Solution **Installing the PCRE2 Library** On Ubuntu/Debian: ```shell sudo apt-get install libpcre2-dev ``` On CentOS/RHEL: ```shell sudo yum install pcre2-devel ``` On the macOS, you can use Homebrew to install: ```shell brew install pcre2 ``` Finally, the solution is perfect. I normally install the 'swoole' extension here ``` Build process completed successfully Installing '/home/linuxbrew/.linuxbrew/Cellar/ php@8.1 /8.1.23/pecl/20210902/swoole.so' Installing '/home/linuxbrew/.linuxbrew/Cellar/ php@8.1 /8.1.23/include/php/ext/swoole/config.h' Installing '/home/linuxbrew/.linuxbrew/Cellar/ php@8.1 /8.1.23/include/php/ext/swoole/php_swoole.h' install ok: channel://pecl.php.net/swoole-5.0.3 Extension swoole enabled in php.ini ```
Brew Install any version of PHP (7.3, 7.4, 8.0, 8.1, 8.2, 8.3) Author: Chuwen Time: 2023-09-19 Classification: Linux comment ##Dependencies * [brew]( https://brew.sh/ "brew") ##Installation First add the 'shivammathur/php' warehouse source: ```shell brew tap shivammathur/php ``` Search for installable PHP versions ` brew search shivammathur/php` ```shell shine@shine :~/work/xxxxx-web$ brew search shivammathur/php ==> Formulae shivammathur/php/php shivammathur/php/ php@7.0-debug shivammathur/php/ php@7.3 shivammathur/php/ php@8.0-debug shivammathur/php/ php@8.4 shivammathur/php/php-debug shivammathur/php/ php@7.1 shivammathur/php/ php@7.3-debug shivammathur/php/ php@8.1 shivammathur/php/ php@8.4-debug shivammathur/php/ php@5.6 shivammathur/php/ php@7.1-debug shivammathur/php/ php@7.4 shivammathur/php/ php@8.1-debug shivammathur/php/ php@5.6-debug shivammathur/php/ php@7.2 shivammathur/php/ php@7.4-debug shivammathur/php/ php@8.3 shivammathur/php/ php@7.0 shivammathur/php/ php@7.2-debug shivammathur/php/ php@8.0 shivammathur/php/ php@8.3-debug ``` For example, if you want these PHP versions, do as follows: * PHP 8.1:`brew install shivammathur/php/ php@8.1 ` * PHP 8.2:`brew install shivammathur/php/ php@8.2 ` * PHP 8.3:`brew install shivammathur/php/ php@8.3 `
MacOS PHP uses Pecl to install the Swoole extension and enable openssl Author: Chuwen Time: 2023-08-26 Classification: PHP comment ##Install PHP You can refer to this tutorial to install: https://stitcher.io/blog/php-82-upgrade-mac#upgrade -with-shivammathur/homebrew-php In order to save time, I extracted some ```shell brew tap shivammathur/php brew install shivammathur/php/ php@8.2 #Switch to PHP 8.2 brew link --overwrite --force php@8.2 ``` ##Install openssl&get the installation path If you have not installed, you can use this command to install ```shell brew install openssl ``` ! []( https://cdn.nowtime.cc/2023/08/26/4241934229.png ) For example, mine is `/usr/local/Cellar/ openssl@3 /3.1.2 ` Please remember this path ##Install the swoole extension&enable openssl support >Make sure your network environment is normal Execute the following command ```shell sudo pecl install swoole ``` When asked if you want to enable openssl, enter 'yes -- with openssl dir=/usr/local/Cell/ openssl@3 /3.1.2` >Where `/usr/local/Cellar/ openssl@3 /3.1.2 is the installation path of openssl ! []( https://cdn.nowtime.cc/2023/08/26/3703705991.png ) For others, just fill in 'yes' OR' no 'according to your own needs, but note that you may need to specify the path of the relevant library. For details, please see [Official Document ->Compilation Options ->General Parameters]( https://wiki.swoole.com/#/environment?id=%e9%80%9a%e7%94%a8%e5%8f%82%e6%95%b0 "Official Documents") ##Installation complete ! []( https://cdn.nowtime.cc/2023/08/26/3470978648.png ) Check whether the installation is successful. Execute the command 'php -- ri swoole'. If there is content output (as shown in the figure), the installation is successful ! []( https://cdn.nowtime.cc/2023/08/26/3318436440.png )