Just to complement, as already said 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 ones, the parameters using type hinting are now supported in this order:
PHP 5.0.0
Class/interface
: The parameter The parameter must be an instance of a given name of a class or interface.
self
: The parameter must be an instance of the same class as the method is defined in. This can only be used in class and instance methods.
PHP 5.1.0
array
The parameter of being an array
PHP 5.4.0
callable
The parameter must a function or method or something equivalent to the same that can be called so $param();
and equals to is_callabe();
PHP 7.0.0
The PHP7 (released on 3/Dec/2015) started to support several types and PHP7 started to be 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 4/Feb/2016), It is strongly recommended not to use 7.0.0, 7.0.1 and 7.0.2.
bool
the must parameter if of the boolean type (true or false).
float
the parameter must be a floating point number (1.1, 2.5, 3.8, etc)
int
the parameter must be of the integer type 1
, 2
, 3
for example.
string
the parameter must be of the string type.
Strict types (Strict)
PHP7 also supported "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
Type return
Also supported in PHP7 the type return, for example:
<?php
function sum($a, $b): float {
return $a + $b;
}
var_dump(sum(1, 2));
This also supports the strict
Work gmsantos, by your attention! I understood perfectly...
– José Neto