Error passing Date as parameter

Asked

Viewed 32 times

-2

I’m creating a class with methods parameterized.

The dice sane fictitious:

<?php

    require_once '../../../erros.php';


    class ClientesModelos {

        private $idClientes, $data, $nome, $sobreNome, $nascimento, $documento, $telefone, $celular, $email, $senha, $bloqueio;

        public function __construct (
            date $_data, 
            string $_nome, 
            string $_sobreNome, 
            date $_nacimento, 
            int $_documento, 
            int $_telefone, 
            bool $_celular, 
            string $_email,
            string $_senha,  
            bool $_bloqueio = false
        ) {         
            $this->data        = $_data;
            $this->nome        = $_nome;
            $this->sobreNome   = $_sobreNome;
            $this->nascimento  = $_nacimento;   
            $this->documento   = $_documento;
            $this->telefone    = $_telefone;
            $this->celular     = $_celular;
            $this->email       = $_email;       
            $this->senha       = $_senha;       
            $this->bloqueio    = $_bloqueio;        
        }       

        public function setIdClientes(int $_idClientes) { $this->idClientes = $_idClientes; }

        public function getIdClientes() : int { return $this->idClientes; }

        public function getData() : string { return $this->data; }

        public function getNome() : string { return $this->nome; }

        public function getSobreNome() : string { return $this->sobreNome; }

        public function getNascimento() : date { return $this->nascimento; }

        public function getDocumento() : int {  return $this->documento; }

        public function getTelefone() : int { return $this->telefone; }

        public function getCelular() : ?bool { return $this->celular ? $this->email : NULL; }

        public function getEmail() : string { return $this->email; }

        public function getSenha() : string { return $this->senha; }

        public function getBloqueio() : string { return $this->bloqueio; }

}


$clientesModelos =  new ClientesModelos(
      date('Y-m-d'),
     'Carlos',
     'Alberto',
     date('1970-12-20'),
     77777777777,
     12345678,
     87654321,
     '[email protected]',
     'aaaa',
     false 
 );


print_r ($clientesModelos);

I’m getting the following error:

Fatal error: Uncaught TypeError: Argument 1 passed to ClientesModelos::__construct() must be an instance of date, string given, called in 
D:\Trabalhos\host\htdocs\mvc_crud_pdo\classes\mvc\modelos\ClientesModelos.php on line 71 and defined in 
D:\Trabalhos\host\htdocs\mvc_crud_pdo\classes\mvc\modelos\ClientesModelos.php:10 Stack trace: #0 
D:\Trabalhos\host\htdocs\mvc_crud_pdo\classes\mvc\modelos\ClientesModelos.php(71): 
ClientesModelos->__construct('2019-05-02', 'Carlos', 'Alberto', '1970-12-20', 77777777777, 12345678, 87654321, '[email protected]....', 'aaaa', false) #1 {main} thrown in D:\Trabalhos\host\htdocs\mvc_crud_pdo\classes\mvc\modelos\ClientesModelos.php on line 10

But I’m going through date correctly.

  • 1

    And what should be the guy date? You’re passing a string.

  • I don’t think so: date("Y-m-d") and date ("1970-12-20"). Where is my error in reasoning?

  • 1

    https://www.php.net/manual/en/function.date.php#refsect1-Function.date-returnvalues

  • Um, would it be the case then for me to switch to string? Or would I have to change the past parameter?]

  • 1

    The mistake is that date is not a type and what you are passing as parameter is the return of the function, which is a string.

  • The correct would then be to switch to string and treat in the body of the function?

Show 1 more comment

1 answer

2


You can use Datetime, so you can define the type

class ClientesModelos {

    private $idClientes, $data, $nome, $sobreNome, $nascimento, $documento, $telefone, $celular, $email, $senha, $bloqueio;

    public function __construct (
        DateTime $_data, 
        string $_nome, 
        string $_sobreNome, 
        DateTime $_nacimento, 
        int $_documento, 
        int $_telefone, 
        bool $_celular, 
        string $_email,
        string $_senha,  
        bool $_bloqueio = false
    ) {         
        $this->data        = $_data;
        $this->nome        = $_nome;
        $this->sobreNome   = $_sobreNome;
        $this->nascimento  = $_nacimento;   
        $this->documento   = $_documento;
        $this->telefone    = $_telefone;
        $this->celular     = $_celular;
        $this->email       = $_email;       
        $this->senha       = $_senha;       
        $this->bloqueio    = $_bloqueio;        
    }       

    public function setIdClientes(int $_idClientes) { $this->idClientes = $_idClientes; }

    public function getIdClientes() : int { return $this->idClientes; }

    public function getData() : string { return $this->data; }

    public function getNome() : string { return $this->nome; }

    public function getSobreNome() : string { return $this->sobreNome; }

    public function getNascimento() : string{ return $this->nascimento->format('Y-m-d\TH:i:s.u'); }

    public function getDocumento() : int {  return $this->documento; }

    public function getTelefone() : int { return $this->telefone; }

    public function getCelular() : ?bool { return $this->celular ? $this->email : NULL; }

    public function getEmail() : string { return $this->email; }

    public function getSenha() : string { return $this->senha; }

    public function getBloqueio() : string { return $this->bloqueio; }

}


$clientesModelos =  new ClientesModelos(
    new DateTime('now'),
    'Carlos',
    'Alberto',
    new DateTime('1970-12-20'),
    1234,
    1234,
    1234,
    '[email protected]',
    'aaaa',
    false 
);


print_r ($clientesModelos);

  • and in getData() I withdraw only the date right?

  • You have to use the object, formatting the date as you want, using ->format()

  • I tried so at the time of popular the object but it did not work $this->data = $_data->date; Because I need the same date to store in the bank

  • use ->format('Y-m-d H:i:s')

  • so ->format('Y-m-d') will give?

  • worked! Thank you!

  • hum, I went to make that way $this->data = new Datetime('now')->format('Y-m-d'); and did not give.

  • this form of error, it is in this class that you tried to do this?

  • but I remembered the famous $this->data = date('Y-m-d');

  • If you want to pass the direct format with DateTime, can do so (new DateTime('now'))->format('Y-m-d')

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.