manuwhat / named-args
name-args - 允许使用命名参数来(定义可调用的函数/调用现有函数)
dev-master
2019-05-30 14:23 UTC
Requires (Dev)
- phpunit/phpunit: ^6.5.0
This package is auto-updated.
Last update: 2024-09-29 05:21:56 UTC
README
允许使用命名参数来(定义可调用的函数/调用现有函数)
要求: PHP 5.3+
命名参数?
这是在其他编程语言中众所周知的概念。PHP 也计划原生支持它。PHP 函数可以有任意数量的参数,参数的定义顺序非常重要。一些参数可能是必需的,一些是可选的。在可选参数之前指定必需参数也很重要。有时有如此多的参数以至于在使用函数时可能会忘记它们的确切顺序。有时在定义函数时,我们需要根据函数未来的使用方便性来选择最优的顺序,特别是当涉及到可选参数的顺序时。有时,在调用函数时,我们需要一些可选参数,但不全部需要,而它们预定义的顺序根本不便于操作。本软件包包含一个通用类,允许定义命名参数函数。它允许以允许按我们想要的任何顺序调用可选和必需参数的方式定义函数。当然,已经有一些软件包试图解决这个问题。然而,这个软件包采用了不同的方法,实际上可以处理任何类型的参数,而不仅仅是那些可以作为字符串指定的参数,并且它还处理引用。它还允许像简单的本地函数一样指定未命名的参数
如何使用它
通过以下命令要求库
composer require manuwhat/named-args
将 require 'vendor/autoload.php'; 添加到脚本的顶部。
require 'Args.php';//require helpers file How to use : let's say that you want to define a function with parameters $first ,$second,$third,$fourth,$fifth where $first and $fourth are required and the other optional: Natively you should do something like function test($first,$fourth ,$second=null,$third=null,$fifth=null){ return $first+$second+$third+$fourth+$fifth; } with this package you must proceed like this: function test(NamedArgs $mandatory){ $required=['first','fourth'];//specify required parameters here $default=['first'=>null,'second'=>null,'third'=>null,'fourth'=>null,'fifth'=>null];//define all parameters required and optional with their default values here extract($mandatory->getParams($required,$default)); unset($mandatory);//gain space //then you can use your parameters as you did before in your functions return $first+$second+$third+$fourth+$fifth; } and for the call you can use: test(Args(['fourth'=>9,'first'=>3,'third'=>79])); or either test(Args([1,2,3])); //just as native function but you will then need to respect predefined order as done natively You can also turn normal functions to named args functions this way: on my PHP version when i do : echo new reflectionFunction('preg_match'); i obtain: Function [ function preg_match ] { - Parameters [5] { Parameter #0 [ $pattern ] Parameter #1 [ $subject ] Parameter #2 [ &$subpatterns ] Parameter #3 [ $flags ] Parameter #4 [ $offset ] } } so i can use the following code to specify my arguments anywhere in the order i want: NamedArgs::preg_match_all(args(['subpatterns'=>&$matches,'pattern'=>'#a|o|i|u|e#','subject'=>'gris|gros|gras|grue'])); or even more simply by removing the NamedArgs object step: NamedArgs::preg_match_all(['subpatterns'=>&$match,'pattern'=>'#a|o|i|u|e#','subject'=>'gris|gros|gras|grue']); Note that i have just called statically preg_match and pass as the only one parameter a NamedArgs object or an array. which print out as you should see when you run var_dump($matches): array(1) { [0]=> array(5) { [0]=> string(1) "i" [1]=> string(1) "o" [2]=> string(1) "a" [3]=> string(1) "u" [4]=> string(1) "e" } } As you can see, few lines of code transform any of your functions to something which will boost your productivity because you won't be forced to search for the exact order in which parameters are defined before call your function and this can be really helpful on big project and you can just switch between named args calling and native calling easily. See the TestNAmedArgs.php file to see a complete how to define functions with references and without references and how to call them .... Keep in mind that although the parameters are required you can specify them in any order for the call... To run unit tests ```bash phpunit ./tests