Date in PHP object orientation?

Asked

Viewed 50 times

0

I’m starting to learn Object Orientation, I’d like to know how to use date with php, because in Java you can import the java util, but in the php I don’t know how to do it, I’ll show you my code:

<?php

abstract class DadosPessoais {
protected $nome, $funcao, $rg, $cpf, $data_adm, $login, $senha;


abstract function logar();  



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

function getFuncao() {
    return $this->funcao;
}

function getRg() {
    return $this->rg;
}

function getCpf() {
    return $this->cpf;
}

function getData_adm() {
    return $this->data_adm;
}

function getLogin() {
    return $this->login;
}

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

function setNome($nome) {
    $this->nome = $nome;
}

function setFuncao($funcao) {
    $this->funcao = $funcao;
}

function setRg($rg) {
    $this->rg = $rg;
}

function setCpf($cpf) {
    $this->cpf = $cpf;
}

function setData_adm($data_adm) {
    $this->data_adm = new DateTime($data_adm);        
}

function setLogin($login) {
    $this->login = $login;
}

function setSenha($senha) {
    $this->senha = $senha;
}
}
  • 1

    You can use the Datetime class

  • I’ve tried using it like this: $funcio ->$this->setData_adm(Datetime($data_adm)); But it doesn’t work, I think I’m missing something.

  • 3

    Only the new. $funcionario ->$this->setData_adm(new DateTime($data_adm)); ;)

  • I get it, but how do I put this line of code when I call an object I have in a constructor? $funcionaio = new Funcionario ("name", "function", rg, Cpf, HERE IS THE DATE, "login", "password");

  • Just put one new DateTime($data_adm)

  • I put this code in the Setters method, but how do I format this date? with day/month/year? Function setData_adm($data_adm) { $this->data_adm = new Datetime($data_adm); } For when I put the above code it returns me down: [data_adm:protected] => 2002-04-04

  • echo $objetoDate->format('d/m/Y');

  • It didn’t work out, actually let me explain better why I’m learning now, I have a working class, in this class there is a constructor that when instantiated I need to put the information as: $name, $function, $rg, $Cpf, $data_adm, $login, $password ...... so when I give a print_r on the screen the date does not appear formatted, I guess I have to format inside the Employee class in the Geter and Setters methods, I tried it in the following way **** Function setData_adm($data_adm) { $this->data_adm = new Datetime($data_adm); } **** But the date appears as year/month/day.

  • Your get will return a Datetime object, that other variable (which gets the return of get) is who will format the date.

  • **It gives error not returning a Datetime object. $funcionario->getData_adm()->format(’d/m/Y'); Fatal error: Call to a Member Function format() on string in C: xampp htdocs Casadasmarmitas index.php on line 20

  • Put the get and set date code in the question. Use the [Edit link].

  • I’ve Already Edited the Question I’ve Put All Get and Set

Show 7 more comments
No answers

Browser other questions tagged

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