In Ruby
def my_func(foo,bar,*zim)
[foo, bar, zim].collect(&:inspect)
end
puts my_func(1,2,3,4,5)
# 1
# 2
# [3, 4, 5]
In PHP (5.3)
function my_func($foo, $bar, ... ){
#...
}
What’s the best way to to do this in PHP?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try
func_get_args— Returns an array comprising a function’s argument listPHP Version of your Ruby Snippet
or just
gives
Note that
func_get_args()will return all arguments passed to a function, not just those not in the signature. Also note that any arguments you define in the signature are considered required and PHP will raise a Warning if they are not present.If you only want to get the remaining arguments and determine that at runtime, you could use the ReflectionFunction API to read the number of arguments in the signature and
array_slicethe full list of arguments to contain only the additional ones, e.g.Why anyone would want that over just using
func_get_args()is beyond me, but it would work. More straightforward is accessing the arguments in any of these ways:If you need to document variable function arguments, PHPDoc suggest to use
Hope that helps.