3

*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.

1
  • 3
    boolean integer float string Scalar types data types.PHP does not support this types hinting. Commented Nov 29, 2012 at 7:47

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

But there is hope wiki.php.net/rfc/typechecking (Under Discussion) and wiki.php.net/rfc/scalar_type_hinting_with_cast (In Draft) or maybe not like wiki.php.net/rfc/parameter_type_casting_hints (Withdrawn).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.