php - how is it that PDO can have methods with typed string parameters, but I can't do this in my own functions? -
if create instance of pdo , call pdo->quote('test') works no problems.
if @ defination of pdo quote method looks this:
/** * quotes string use in query. * pdo::quote() places quotes around input string (if required) , escapes special characters within input string, using quoting style appropriate underlying driver. * * @param string $string string quoted. * @param int $parameter_type provides data type hint drivers have alternate quoting styles. * * return string */ function quote(string $string, int $parameter_type) {/* method implementation */} note parameters actully have types defined in method signature, string , int.
now if create function this:
function test(string $test) { return $test; } and attempt call this:
echo test('test'); it fails following error:
( ! ) catchable fatal error: argument 1 passed test() must instance of string, string given, called in [path_removed]testtypehinting.php on line 36 , defined in [path_removed]testtypehinting.php on line 2 how come pdo can it, can't?
regards,
scott
it's documentation , real code. read type hinting.
type hints can not used scalar types such int or string
but there moving implement scalar type hinting.
you can add phpdoc documentation function.
/** * test function * @param string $test * @return string */ function test($test) { return $test; } also read how read function definition
Comments
Post a Comment