I have a problem with a constructor that I am trying to create in php, I don't understand why I get a syntax error when I write the constructor in this way:
I don't understand is it due to the version of php or what am I doing wrong, now if I remove all the public on screen from the constructor it prints the following error:
Catchable fatal error: Argument 1 passed to Product::__construct() must be an instance of string, string given
I don't understand that error either, my class and my function are being called in this way:
$producto = new Producto('Teclado', 200, true);
$producto->mostrarProducto();
echo "<pre>";
var_dump($producto);
echo "</pre>";
The
public
ones in the parameters are too much for you, that's clear.And the types (
string
,int
,bool
) are also left over if the version of PHP running the script is older than PHP 7 .Prior to PHP 7, type hinting could only be used to coerce object and array types. In the case of your constructor, an object of class string is expected, but a (scalar) string is being passed to it.
https://stackoverflow.com/questions/4103480/how-to-resolve-must-be-an-instance-of-string-string-given-prior-to-php-7