What is the difference of these parameters (array) in these methods?

12

I see this a lot in methods

class Exemplo
{
    public function exemplo1(array $parameters = array())
    {
    }

    public function exemplo2(array $parameters)
    {
    }

    public function exemplo3($parameters = array())
    {
    }
}

This means that $parameters is an array , but what's the difference between these implementations?

    
asked by anonymous 30.01.2015 / 14:55

7 answers

12

Two important concepts that you need to understand are type hinting and parameter default value .

Type hinting forces that argument passed has a specific type (in this case a array ).

As of PHP 7 we can do type hinting from primitive types ( int , string , float ).

Type hinting is limited to Objects, Interfaces, Arrays, and Closures .

Now the default value of the parameter is the value that the parameter will take if no is passed to that parameter. Understand as an optional parameter.

Taking your examples, you would then have:

// Um método que obriga que o argumento seja um array 
// mas aceita uma chamada sem parâmetros:
public function exemplo1(array $parameters = array()){ }

$obj->exemplo1();

// Obriga que o argumento seja um array, e que o parâmetro seja passado: 
public function exemplo2(array $parameters){ }

$obj->exemplo2(array('id' => 1));

// Se nada for passado como argumento, assumirá o valor de um array vazio, 
// mas te permite passar outros tipos como argumento
public function exemplo3($parameters = array()){ }

$obj->exemplo3("Eu nao sou um array, mas tudo bem!");

Passing a default value that does not satisfy type hinting will return an error:

// Type hinting de array e valor padrão string
public function test(array $teste = "array")
  

PHP Fatal error: Default value for parameters with array type hint can only be an array or NULL

    
30.01.2015 / 15:21
9

The first two are explicitly indicating that the parameter should receive an array . The latter does not, a value of any kind can be passed. This is called type hinting in PHP.

The first and last initialize the parameter with an array empty if no value is passed to it. The priority will be of the passed argument if it exists.

    
30.01.2015 / 15:08
7

This is my understanding

 public function exemplo2(array $parameters)
  

The function is forcing the programmer to pass array .


public function exemplo3($parameters = array())
  

The function is setting the default value to array , but the programmer can pass anything.


 public function exemplo1(array $parameters = array())
  

The only accepts values of type array and the optional and fill , if the programmer does not pass will use the array()

    
30.01.2015 / 15:10
5

This first method sets $parameters to be an array if a value other than this is generated, if no argument is passed parameters will be treated as an empty array.

public function exemplo1(array $parameters = array())

Example:

$e = new Exemplo();
$e->exemplo1(1);

Error generated:

  

Catchable fatal error: Argument 1 passed to Example: example1 () must   be of the type array, integer given, called

Requires that parameters is an array, otherwise it generates the same error as above, and its default value is NULL and not an empty array as exemplo1() .

public function exemplo2(array $parameters)

Defines that if no argument is passed $parameters will be an empty array, this method accepts other values such as a string or integer.

public function exemplo3($parameters = array())

The type defined on the left side of the parameter is called type hinting , which ensures that the defined type must be respected if something different comes up an error is thrown. But it is not possible to type parametres with primitive types (int, float, sting), int or string. Resources and Traits. Usually the name of a class or interface is placed.

    
30.01.2015 / 15:09
3

In the function exemplo1 the array is to indicate that the parameter $parameters should be an array. If the function is called with a parameter of another type, PHP will generate a Warning warning that the passed type is incorrect.

The = array() part is used to initialize the parameter value, which makes it optional at the time of calling the function.

    
30.01.2015 / 15:11
3

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

  • Class/interface : The parameter must be an instance of the same class that the method is defined in. This can only be used in class and instance methods.

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

    
28.02.2016 / 17:26
0

Explanation of methods

Method exemplo1 : expects a value of type array, in addition, this method is receiving a default value that is an empty array, which makes the parameter optional. >

Method exemplo2 : In this method, a value of type array is expected. The parameter is not optional in this case.

Method exemplo3 : The method accepts any type of value, since no type hinting has been defined. In addition, the parameter in question is optional and is receiving an array as the default value, however it is not restricted to only receiving arrays.

    
16.09.2018 / 21:31