PHP Function Arguments - Use an array or not?

前端 未结 9 1545
挽巷
挽巷 2021-02-04 11:32

I like creating my PHP functions using key=>value pairs (arrays) as arguments instead of individual parameters.

For example, I prefer:

function useless_         


        
9条回答
  •  庸人自扰
    2021-02-04 12:06

    Well, it's kinda usefully. But for some arguments which is passing always it's better to use classic passing like function some($a1, $a2). I'm doing like this in my code:

    function getSome(SomeClass $object, array $options = array())
    {
        // $object is required to be an instance of SomeClass, and there's no need to get element by key, then check if it's an object and it's an instance of SomeClass
    
        // Set defaults for all passed options
        $options = array_merge(array(
            'property1' => 'default1',
            'property2' => 'default2',
            ... => ...
        ), $options); 
    }
    

    So, as you can see I like that code style too, but for core-arguments I prefer classic style, because that way PHP controls more things which should I, if I used the you code style.

提交回复
热议问题