How to resolve “must be an instance of string, string given” prior to PHP 7?

后端 未结 9 1196
情话喂你
情话喂你 2020-11-27 10:32

Here is my code:

function phpwtf(string $s) {
    echo \"$s\\n\";
}
phpwtf(\"Type hinting is da bomb\");

Which results in this error:

相关标签:
9条回答
  • 2020-11-27 10:59

    From PHP's manual :

    Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported.

    So you have it. The error message is not really helpful, I give you that though.

    ** 2017 Edit **

    PHP7 introduced more function data type declarations, and the aforementioned link has been moved to Function arguments : Type declarations. From that page :

    Valid types

    • Class/interface name : The parameter must be an instanceof the given class or interface name. (since PHP 5.0.0)
    • self : The parameter must be an instanceof the same class as the one the method is defined on. This can only be used on class and instance methods. (since PHP 5.0.0)
    • array : The parameter must be an array. (since PHP 5.1.0) callable The parameter must be a valid callable. PHP 5.4.0
    • bool : The parameter must be a boolean value. (since PHP 7.0.0)
    • float : The parameter must be a floating point number. (since PHP 7.0.0)
    • int : The parameter must be an integer. (since PHP 7.0.0)
    • string : The parameter must be a string. (since PHP 7.0.0)
    • iterable : The parameter must be either an array or an instanceof Traversable. (since PHP 7.1.0)

    Warning

    Aliases for the above scalar types are not supported. Instead, they are treated as class or interface names. For example, using boolean as a parameter or return type will require an argument or return value that is an instanceof the class or interface boolean, rather than of type bool:

    <?php
       function test(boolean $param) {}
       test(true);
     ?>
    

    The above example will output:

     Fatal error: Uncaught TypeError: Argument 1 passed to test() must be an instance of boolean, boolean given, called in - on line 1 and defined in -:1
    

    The last warning is actually significant to understand the error "Argument must of type string, string given"; since mostly only class/interface names are allowed as argument type, PHP tries to locate a class name "string", but can't find any because it is a primitive type, thus fail with this awkward error.

    0 讨论(0)
  • 2020-11-27 11:03

    I think typecasting on php on inside block, String on PHP is not object as I know:

    <?php
    function phpwtf($s) {
        $s = (string) $s;
        echo "$s\n";
    }
    phpwtf("Type hinting is da bomb");
    
    0 讨论(0)
  • 2020-11-27 11:04

    (originally posted by leepowers in his question)

    The error message is confusing for one big reason:

    Primitive type names are not reserved in PHP

    The following are all valid class declarations:

    class string { }
    class int { }
    class float { }
    class double { }
    

    My mistake was in thinking that the error message was referring solely to the string primitive type - the word 'instance' should have given me pause. An example to illustrate further:

    class string { }
    $n = 1234;
    $s1 = (string)$n;
    $s2 = new string();
    $a = array('no', 'yes');
    printf("\$s1 - primitive string? %s - string instance? %s\n",
            $a[is_string($s1)], $a[is_a($s1, 'string')]);
    printf("\$s2 - primitive string? %s - string instance? %s\n",
            $a[is_string($s2)], $a[is_a($s2, 'string')]);
    

    Output:

    $s1 - primitive string? yes - string instance? no

    $s2 - primitive string? no - string instance? yes

    In PHP it's possible for a string to be a string except when it's actually a string. As with any language that uses implicit type conversion, context is everything.

    0 讨论(0)
  • 2020-11-27 11:05

    As of PHP 7.0 type declarations allow scalar types, so these types are now available: self, array, callable, bool, float, int, string. The first three were available in PHP 5, but the last four are new in PHP 7. If you use anything else (e.g. integer or boolean) that will be interpreted as a class name.

    See the PHP manual for more information.

    0 讨论(0)
  • 2020-11-27 11:06

    Maybe not safe and pretty but if you must:

    class string
    {
        private $Text;
        public function __construct($value)
        {
            $this->Text = $value;
        }
    
        public function __toString()
        {
            return $this->Text;
        }
    }
    
    function Test123(string $s)
    {
        echo $s;
    }
    
    Test123(new string("Testing"));
    
    0 讨论(0)
  • 2020-11-27 11:13

    I got this error when invoking a function from a Laravel Controller to a PHP file.

    After a couple of hours, I found the problem: I was using $this from within a static function.

    0 讨论(0)
提交回复
热议问题