I have a little problem with the constructor of my project and it is that after making a query to the server and returning the number of variables that the constructor asks me for, it tells me that it only sends 1 and that 5 are expected.
Mistake:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Candidato::__construct(), 1 passed in C:\xampp\htdocs\recuPHP\MVC\Final\Model\base.php on line 65 and exactly 5 expected in C:\xampp\htdocs\recuPHP\MVC\Final\Model\candidato.php:20 Stack trace: #0 C:\xampp\htdocs\recuPHP\MVC\Final\Model\base.php(65): Candidato->__construct(Array) #1 C:\xampp\htdocs\recuPHP\MVC\Final\Controller\index.php(5): ClaseExamen::obtieneCandidatos() #2 {main} thrown in C:\xampp\htdocs\recuPHP\MVC\Final\Model\candidato.php on line 20
I have the Candidate class:
class Candidato {
protected $dni;
protected $nombre;
protected $apellidos;
protected $sexo;
protected $idiomas; //array de idiomas
public function getDni() {return $this->dni; }
public function getNombre() {return $this->nombre;}
public function getApellidos() {return $this->apellidos;}
public function getSexo() {return $this->sexo;}
public function getIdiomas() {return $this->idiomas;}
public function __construct($pdni,$pnombre,$papellidos,$psexo,$pidiomas) {
$this->dni = $pdni;
$this->nombre = $pnombre;
$this->apellidos=$papellidos;
$this->sexo=$psexo;
$this->idiomas=$pidiomas;
And I have the function that performs the query:
public static function obtieneCandidatos()
{
$sql2= "SELECT c.dni, c.nombre, c.apellidos, c.sexo, id.nombre FROM candidatos c
inner join idiomas_encuesta i on c.dni=i.dni_candidato
inner join idiomas id on i.id_idioma=id.id;";
$resultado2=self::ejecutaConsulta($sql2);
$arrayCandidatos=array();
while ($fila=$resultado2->fetch(PDO::FETCH_ASSOC))
{
$arrayCandidatos[]=new Candidato ($fila);
}
return $arrayCandidatos;
}
When I want to call the function from the controller:
$arrayDatosPersonas=ClaseBase::obtieneCandidatos();
It gives me the error, in theory I am sending the 5 attributes but it seems that it does not detect them. In the language table there are several languages and I don't know if that is the problem. I hope you can help me thanks.
You are sending an array:
So your constructor must be able to receive the array:
Or instead of modifying the
contruct
modify the instance so that it has the ability to send the individual data:Being an associative Array, you cannot use the
spread
...$array operator for this to work you would have to force it witharray_values
( Update from version 8 of PHP if it works with associative arrays):Related documentation: