0
I have the Model class down below:
namespace classes\mvc\modelos;
use \DateTime;
class ClientesModelos {
private $idClientes, $data, $nome, $sobreNome, $nascimento;
public function __construct (
string $_nome,
string $_sobreNome,
DateTime $_nacimento
) {
$this->data = date('Y-m-d');
$this->nome = $_nome;
$this->sobreNome = $_sobreNome;
$this->nascimento = $_nacimento;
}
public function setIdClientes(int $_idClientes) { $this->idClientes = $_idClientes; }
public function setData(DateTime $_data): DateTime { $this->data = $_data; }
public function getIdClientes() : int { return $this->idClientes; }
public function getData() : DateTime { return $this->data; }
public function getNome() : string { return $this->nome; }
public function getSobreNome() : string { return $this->sobreNome; }
public function getNascimento() : DateTime { return $this->nascimento; }
}
And also the Classe Dao with the method insert:
public function inserir (ClientesModelos $_cliente) : bool {
$sql = 'INSERT INTO clientes (data, nome, sobreNome, nascimento, documento, telefone, celular, email, senha, bloqueio) VALUES (?,?,?,?,?,?,?,?,?,?)';
$inserir = $this->pdo->prepare ($sql);
$inserir->bindValue (1, $_cliente->getData());
$inserir->bindValue (2, $_cliente->getNome());
$inserir->bindValue (3, $_cliente->getSobreNome());
$inserir->bindValue (4, $_cliente->getNascimento()->format("Y-m-d"));
return $inserir->execute();
}
I summarized at the maximum to get just the necessary to the reply of doubt.
The problem:
When squeegee to index php.
$cliente = new ClientesModelos (
'Carlos',
'Alberto',
new DateTime('1970-12-20')
);
$clienteControle = new ClientesControles();
$clienteControle->inserir ($cliente);
Method of Control class:
public function inserir(ClientesModelos $_pessoa) : string {
if ( !is_object($_pessoa) ) return 'Uma pessoa precisa ser passada como parâmtro' ;
else return $this->ClientesDao->inserir($_pessoa) ? 'Cadastro efetuado com sucesso' : 'Erro no processo do cadastro';
}
Method insert of class Dao:
public function inserir (ClientesModelos $_cliente) : bool {
$sql = 'INSERT INTO clientes (data, nome, sobreNome, nascimento, documento, telefone, celular, email, senha, bloqueio) VALUES (?,?,?,?,?,?,?,?,?,?)';
$inserir = $this->pdo->prepare ($sql);
$inserir->bindValue (1, $_cliente->getData());
$inserir->bindValue (2, $_cliente->getNome());
$inserir->bindValue (3, $_cliente->getSobreNome());
$inserir->bindValue (4, $_cliente->getNascimento()->format("Y-m-d"));
return $inserir->execute();
}
I get the next error:
Fatal error: Uncaught Typeerror: Return value of classes mvc templates Customersmodels::getData() must be an instance of Datetime, string returned in D: Host jobs htdocs mvc_crud_pdo classes mvc templates Customersdelos.php:40 Stack trace: #0 D: Jobs host htdocs mvc_crud_pdo classes mvc templates Clientesdao.php(20): classes mvc templates Clientsmodelos->getData() #1 D: Jobs host htdocs mvc_crud_pdo classes mvc controls Clientscontroles.php(22): classes mvc templates Clientsdao->insert(Object(classes mvc models Clientesmodelos)) #2 D: Host jobs htdocs mvc_crud_pdo classes mvc visoes Clientesvisoes.php(31): classes mvc controls Clientescontroles->insert(Object(classes mvc templates Clientesmodelos)) #3 D: Host jobs htdocs mvc_crud_pdo index.php(18): classes mvc visoes Clientesvisoes->html() #4 {main} thrown in D: Host jobs htdocs mvc_crud_pdo classes mvc templates Clientesmodelos.php on line 40
However the object returns the Datetime correctly.
The function
date
, despite the name, returns a string. Then$this->data
is a string, but the methodgetData
indicates that the return is aDateTime
. Then either you change the type of the variable (assign the valuenew DateTime()
for example), or the type the method returns, so that both are the same...– hkotsubo
ok, thank you! The solution of the answer below cleared my mind!
– Carlos Rocha