Does anyone know why it fails?
<?php
function data((string)$data)
{
echo $data;
}
data(123);
Here is the error:
Parse error: syntax error, unexpected T_STRING_CAST, expecting '&' or T_VARIABLE in x.php on line 3
Does anyone know why it fails?
<?php
function data((string)$data)
{
echo $data;
}
data(123);
Here is the error:
Parse error: syntax error, unexpected T_STRING_CAST, expecting '&' or T_VARIABLE in x.php on line 3
What your are trying to do is called: Type Hinting.
That is allowing a function or class method to dictate the type of argument it recieves.
Static type hinting was only introduced in PHP7 so using a version greater than that you can achieve whay you want with the following:
<?php
function myFunction(string $string){
echo $string;
}
Now any non string argument passed to myFunction will throw an error.