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

后端 未结 9 1247
情话喂你
情话喂你 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 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"));
    

提交回复
热议问题