php作为动不动搞个大事情世界上最好的语言,经常偶尔会出现由于版本升级导致的不兼容问题。笔者在工作中遇到了php7.1
升级到php7.4
导致的each
弃用、mcrypt
库启动导致的不兼容。在这里备注下兼容方式。
#
each弃用
从php7.2
开始,官方开始弃用each
函数。作为一个伪码农我是很震惊的,无法想象哪天python
把range
弃用了,代码维护人员是否有毅力将所有的历史内容都重新适配下。php官方
就是这么任性自信,直接删除,不做兼容。网上找到的兼容方案是这样的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| function func_new_each(&$array){
$res = array();
$key = key($array);
if($key !== null){
next($array);
$res[1] = $res['value'] = $array[$key];
$res[0] = $res['key'] = $key;
}else{
$res = false;
}
return $res;
}
// 替换前
list($scalar_type, $scalar_value) = each($methName->me);
// 替换后
list($scalar_type, $scalar_value) = func_new_each($methName->me);
|
#
mcrpt库弃用
这个更狠了,整个库直接弃用。改为推荐openssl
。好在工程不是长期维护的,后续可能还有重构的规划,所以以解决问题为优先目标吧,直接将废弃的mcrypt
作为插件,重新编入php7.4
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
| #! /bin/bash
# any problem please contact me
# used to install mcrypt.so extentions for php
php_path=${1:-"/usr/local/php"};
echo "install php under ${php_path}";
function Info(){
echo `whoami`@`hostname`:`pwd`;
}
function check_env(){
mised=1;
for i in {0}; do
set -x;
command -v ${php_path}/bin/php >/dev/null 2>&1 || break
command -v ${php_path}/bin/phpize >/dev/null 2>&1 || break
command -v ${php_path}/bin/php-config >/dev/null 2>&1 || break
set +x;
mised=0;
done
return ${mised};
}
check_env;
if [ $? -ne 0 ]; then
echo "missing php exe file";
exit 233;
fi
# wget mcrypt
wk_dir="${HOME}/php_extend";
mkdir -p ${wk_dir};
if [ $? -ne 0 ]; then
echo "cannot create ${wk_dir}, check permission";
exit 20;
fi
rm -rf ${wk_dir}/*;
cd ${wk_dir} && wget http://pecl.php.net/get/mcrypt-1.0.4.tgz;
if [ $? -ne 0 ]; then
echo "download mcrypt-1.0.4 failed";
exit 23;
fi
# prepare
cd ${wk_dir} && tar -xvf mcrypt-1.0.4.tgz;
cd ${wk_dir}/mcrypt-1.0.4 && ${php_path}/bin/phpize;
if [ $? -ne 0 ]; then
echo "mcrypt path init failed";
exit 233;
fi
# configure
cd ${wk_dir}/mcrypt-1.0.4 && ./configure --prefix=${wk_dir}/mcrypt --with-php-config=${php_path}/bin/php-config;
if [ $? -ne 0 ]; then
echo "=========> attention that, configure failed, would try to make";
fi
# XXX: there is a sudo
cd ${wk_dir}/mcrypt-1.0.4 && make && sudo make install
# check if has output
extend_dir=$(${php_path}/bin/php-config --extension-dir);
if [ ! -f "${extend_dir}/mcrypt.so" ]; then
echo "======> mcrypt.so generate failed";
exit 233;
fi
ini_dir=$(${php_path}/bin/php-config --ini-path);
if [ ! -f "${ini_dir}/php.ini" ]; then
echo "======> missing php ini file, ${ini_dir}/php.ini";
exit 233;
fi
cat << EOF
mcrypt.so is generated in ${extend_dir}/mcrypt.so,
please do with sudo permission:
echo "extension=mcrypt.so" >> ${ini_dir}/php.ini
EOF
# XXX: there is a sudo
# need sudo, and should run ok
#sudo echo "extension=mcrypt.so" >> ${ini_dir}/php.ini
#if [ $? -ne 0 ]; then
# echo "cannot write `extension=mcrypt.so` into ${ini_dir}/php.ini, please check permission";
# exit 2333;
#fi
#
#echo "mcrypt.so install ok";
|
语言的兼容性确实像噩梦一样。希望不要老是整这些幺蛾子。
#
参考文献
php7.2 废弃each方法
php7.2 安装 mcrypt扩展