php7的写法要求更加严谨
直接看代码
<?php
class test {
public static function test1($name, $name1)
{
echo $name . "-" . $name1;
}
}
$testArr = array("test" , "test1");
$test = new test();
$test->$testArr[1]("bill", "test"); // 这行代码有问题
这段代码在php5下面运行的结果是 bill-test.没啥问题
但是在php7下面会报错
在php7下面有几种改进。(本身以上写法就有问题,但是调试起来不容易发现)
应该改为:
$test->{$testArr[1]}("bill", "test");
或者更加规范的写法:
call_user_func_array(array($test, $testArr[1]), array("bill", "test"));
本文探讨了从 PHP5 到 PHP7 的代码兼容性问题,特别关注了对象方法调用的不同写法及其在 PHP7 中的正确实现方式。
156

被折叠的 条评论
为什么被折叠?



