Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please explain this PHP Function Definition

Tags:

function

php

*Please change the title into something that refer into this question since i didn't know what is this should be.

When i read some tutorial i see script like this

function testArray(array $categories) {
var_dump($categories);
}

testArray(array('string'));

The array in function definition make the parameter strict must be array, which i love it.
Then i test this script

function testString(string $string) {
    var_dump($string);
}

testString('test');

Why did i get error like this Catchable fatal error: Argument 1 passed to testString() must be an instance of string, string given ....?
I'm pretty sure the parameter is string.

like image 803
GusDeCooL Avatar asked Mar 07 '26 22:03

GusDeCooL


1 Answers

You are talking about Type Hinting. PHP type hinting can only be used in objects and arrays. String is a scalar type which does not supports type hinting

More on type hinting

http://php.net/manual/en/language.oop5.typehinting.php

like image 193
coolguy Avatar answered Mar 10 '26 11:03

coolguy