The array keyword before function parameters in PHP

I saw a function declared as follows:

public static function factory($file=null, array $data=null, $auto_encode=null) 

If you want to see the real class, go to this view.php class from the plug of the fuel filter perfume package in github.

My question is: what does the keyword array in array $data = null mean?

+4
source share
3 answers

This is an example of a tip like PHP5 . The $data parameter must be an array. You can specify method parameters for both the object and array types. If it is an object, you can specify the class name as the hint keyword.

+4
source

For this, the argument must be an array. See http://php.net/manual/en/language.oop5.typehinting.php

+3
source

If I remember correctly, you can set the class name in front of the parameter to limit the type of the variable to the class, however, I'm not sure about the array actual use here.

EDIT: Apparently my PHP is a bit rusty. According to http://php.net/manual/en/language.oop5.typehinting.php :

PHP 5 introduces Type Hinting. Functions can now force objects to be objects (by specifying the class name in the function prototype) or arrays (starting with PHP 5.1). However, if NULL is used as the default parameter value, it will be resolved as an argument for any subsequent call.

For instance:

 // An example class class MyClass { /** * A test function * * First parameter must be an object of type OtherClass */ public function test(OtherClass $otherclass) { echo $otherclass->var; } /** * Another test function * * First parameter must be an array */ public function test_array(array $input_array) { print_r($input_array); } } // Another example class class OtherClass { public $var = 'Hello World'; }
// An example class class MyClass { /** * A test function * * First parameter must be an object of type OtherClass */ public function test(OtherClass $otherclass) { echo $otherclass->var; } /** * Another test function * * First parameter must be an array */ public function test_array(array $input_array) { print_r($input_array); } } // Another example class class OtherClass { public $var = 'Hello World'; } 
+1
source

All Articles