Required parameter $xxx follows optional parameter $yyy

22發表於2021-03-11

This style of function declaration has been deprecated in PHP 8.0 . Writing functions like this has never made sense, since all parameters (up to the last required one) would need to be specified when the function was called. The new deprecation simply ensures that function signatures follow the common sense assumption that required parameters, which must appear, should always be declared before optional ones.
The function should be rewritten to remove the default value on the earlier parameters. Since the function could never be called without declaring all parameters, this should have no effect on its functionality.

<?php
    function test($a = [], $b) {} // Before
    function test($a, $b) {}      // After
?>
本作品採用《CC 協議》,轉載必須註明作者和本文連結
www.haowuliaoa.com

相關文章