Just to complement, as already stated in the other answers:
//O primeiro parâmetro deve ser array ou vazio (caso vazio assume 'array()')
public function exemplo1(array $parameters = array())
//O primeiro parâmetro deve ser um array e não pode ser vazio
public function exemplo2(array $parameters)
//O primeiro parâmetro deve ser de qualquer tipo, se vazio assume 'array()'
public function exemplo3($parameters = array())
Now the additional, parameters using type hinting are now supported in this order:
PHP 5.0.0
PHP 5.1.0
-
self
The parameter to be an array
PHP 5.4.0
-
array
Parameter must be a function or method or something equivalent to the same that can be called callable
and is equivalent to $param();
PHP 7.0.0
PHP7 (released Dec 3, 2015) now supports several types and PHP7 is now called Type declarations and started to generate a TypeError exception when declared a parameter of a different type.
Currently it is in the version 7.0.3 (released on Feb 4, 2016) , it is highly recommended that you do not use 7.0.0, 7.0.1 and 7.0.2.
-
% with_the% parameter should be boolean (true or false).
-
% with% parameter must be a floating-point number (1.1, 2.5, 3.8, etc.)
-
is_callabe();
parameter must be integer type bool
, float
, int
for example.
-
1
the parameter must be of type string.
Strict (strict) types
PHP7 also supports "strict mode" for type declaration, for example:
<?php
declare(strict_types=1);
function sum(int $a, int $b) {
return $a + $b;
}
var_dump(sum(1, 2));
var_dump(sum(1.5, 2.5));
The result will be this error:
int(3)
Fatal error: Uncaught TypeError: Argument 1 passed to sum() must be of the type integer, float given, called in - on line 9 and defined in -:4
Stack trace:
#0 -(9): sum(1.5, 2.5)
#1 {main}
thrown in - on line 4
Return type
PHP7 has also been supported in the type return, for example:
<?php
function sum($a, $b): float {
return $a + $b;
}
var_dump(sum(1, 2));
This also supports 2