jamil
jamil

Reputation: 555

Purpose of stating "array" without parentheses?

I got a code snippet from here before and was curious about this syntax that I originally copy-pasted:

function create(array &$data){
  #blah blah...
}

What is the purpose of array &$data as opposed to &$data by itself? In other words, what does the statement array do in this context?

Upvotes: 0

Views: 41

Answers (1)

John Conde
John Conde

Reputation: 219824

It's called type hinting:

PHP 5 introduces type hinting. Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype), interfaces, arrays (since PHP 5.1) or callable (since PHP 5.4). However, if NULL is used as the default parameter value, it will be allowed as an argument for any later call.

If class or interface is specified as type hint then all its children or implementations are allowed too.

Type hints can not be used with scalar types such as int or string. Traits are not allowed either.

Upvotes: 3

Related Questions