I saw a function declared like this:
public static function factory($file=null, array $data=null, $auto开发者_JAVA技巧_encode=null)
If you want to see the real class, please go to this view.php
class from a fork of fuelphp parser package in github.
My question is, what does array
keyword means in array $data = null
?
This is an example of PHP5's type hinting. The parameter $data
is supposed to be an array. It is possible to hint method parameters as either object
or array
types. If it is an object, you can specify the name of the class as the hint keyword.
It requires the argument to be an array. See http://php.net/manual/en/language.oop5.typehinting.php
If I recall correctly, you can set a class name before a parameter to limit the variable type to that of the class, however, I'm not sure of array
's valid 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 are now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays (since PHP 5.1). However, if NULL is used as the default parameter value, it will be allowed as an argument for any later call.
For example:
// 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';
}
精彩评论