How to Resolve "Must Be an Instance of String, String Given" Prior to PHP 7

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

Prior to PHP 7 type hinting can only be used to force the types of objects and arrays. Scalar types are not type-hintable. In this case an object of the class string is expected, but you're giving it a (scalar) string. The error message may be funny, but it's not supposed to work to begin with. Given the dynamic typing system, this actually makes some sort of perverted sense.

You can only manually "type hint" scalar types:

function foo($string) {
if (!is_string($string)) {
trigger_error('No, you fool!');
return;
}
...
}

Getting an error for double and boolean even boolean given

There is no double type hinting. And boolean should be changed to bool.
Your PHPDoc controls didn't match te parameters too

     /**
* [getData description]
* @param array $data [description]
* @param string $a [description]
* @param int $b [description]
* @param double $f [description]
* @param float $d [description]
* @param bool $c [description]
* @return [type] [description]
*/

public function getData(array $data, string $a, int $b, $f, float $d, bool $c)
{

}

$a->getData(["as"],"assasa",12345,65.41,64.153454 , true);

PHP TypeError: Parameter must be an instance of lib\util\mixed, string given

mixed is not a valid type for hinting in PHP. The valid types are listed here: http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration

The slightly cryptic error message is because PHP is assuming you are trying to restrict the type to a class within your namespace: lib\util\mixed.

If you don't want to restrict the type, just don't add a type hint.

Error when passing string into method with type hinting

Just remove string from constructor (not supported) , it should work fine eg:

function __construct($anAnswer)
{
$this->theAnswer = $anAnswer;
}

Working Example:

class Question
{
/**
* The answer to the question.
* @access private
* @var string
*/
public $theAnswer;

/**
* Creates a new question with the specified answer.
* @param string $anAnswer the answer to the question
*/
function __construct($anAnswer)
{
$this->theAnswer = $anAnswer;
}
}

$question = new Question("An Answer");
echo $question->theAnswer;

Type Hints not working for strings in function in php 7

This is because by default, PHP will coerce values of the wrong type into the expected scalar type if possible. For example, a function that is given an integer for a parameter that expects a string will get a variable of type string.

see here

If you would use values that could be cast in your second example it would work:

function def_arg(int $name, int $address, string $test){
return $name . $address . $test;
}

echo def_arg("12", "22", 1) ;

This is because those values can be cast from string to int and vise versa.

It is possible to enable strict mode on a per-file basis. In strict mode, only a variable of exact type of the type declaration will be accepted, or a TypeError will be thrown. The only exception to this rule is that an integer may be given to a function expecting a float. Function calls from within internal functions will not be affected by the strict_types declaration.

Laravel Typer error: Argument 1 passed to must be an instance of . string given

It seems that you are passing $cliente_id as the id of the client not as instance of a Client.

Try to find the Client first:

public function updateTelefono(ClienteRequest $request, $cliente_id, $telefono_id)
{
$cliente = \App\Models\Cliente\Cliente::find($cliente_id);
$telefono = \App\Models\Cliente\Telefono::find($telefono_id);
$this->clienteRepository->updateTelefono($cliente, $telefono, $request->only(
'telefono',
'tipo_telefono_id',
'comentario'
));

return redirect()->to(route('admin.clientes.show', $cliente_id) . '#telefonos')->withFlashSuccess(__('Teléfono actualizado correctamente.'));
}


Related Topics



Leave a reply



Submit