6
I have a code and would like to know if it is an object-oriented code. It follows the structure. I work with 3 files.
first flame save.php
, follows the code:
include_once('dao.php');
include_once('modelo.php');
$post = new Post();
$post->setDescricao($_POST['custo_descricao_html']);
$post->setMes($_POST['custo_mes_atual']);
$post->setLocal($_POST['custo_local_html']);
$post->setPreco($_POST['custo_custo_html']);
$post->setObservacao($_POST['custo_observacao_html']);
$post->setColaborador_id($_POST['colaborador_logado_custo']);
$dao = new DaoCusto();
if ($_POST['custo_controle_html'] == 0) {
$post->setId(rand(0, time()));
$resultado = $dao->insert($post);
echo($resultado == 'salvo') ? '0' : $resultado;
} else {
$post->setId($_POST['custo_idUpdate_html']);
$resultado = $dao->update($post);
echo($resultado == 'salvo') ? '0' : $resultado;
}
2nd file I make the gets
and sets
, modelo.php
:
<?php
class Post {
private $id;
private $descricao;
private $mes;
private $local;
private $preco;
private $observacao;
private $colaborador_id;
public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
return $this;
}
public function getDescricao() {
return $this->descricao;
}
public function setDescricao($descricao) {
$this->descricao = $descricao;
return $this;
}
public function getMes() {
return $this->mes;
}
public function setMes($mes) {
$this->mes = $mes;
return $this;
}
public function getLocal() {
return $this->local;
}
public function setLocal($local) {
$this->local = $local;
return $this;
}
public function getPreco() {
return $this->preco;
}
public function setPreco($preco) {
$this->preco = $preco;
return $this;
}
public function getObservacao() {
return $this->observacao;
}
public function setObservacao($observacao) {
$this->observacao = $observacao;
return $this;
}
public function getColaborador_id() {
return $this->colaborador_id;
}
public function setColaborador_id($colaborador_id) {
$this->colaborador_id = $colaborador_id;
return $this;
}
}
And the third file that’s where I do mine CRUD
, dao.php
<?php
require_once '../../../conexao/conexao.php';
require_once 'modelo.php';
class DaoCusto {
public static $instance;
public function __construct() {
}
public function insert(Post $custo) {
try {
$sql = "INSERT INTO tb_custo (custo_id , custo_descricao , custo_mes , custo_local , custo_preco , custo_observacao , colaborador_colaborador_id) values (:custo_id , :custo_descricao , :custo_mes , :custo_local , :custo_preco , :custo_observacao , :colaborador_colaborador_id)";
$p_sql = Conexao::getInstance()->prepare($sql);
$p_sql->bindValue(':custo_id', $custo->getId());
$p_sql->bindValue(':custo_descricao', $custo->getDescricao());
$p_sql->bindValue(':custo_mes', $custo->getMes());
$p_sql->bindValue(':custo_local', $custo->getLocal());
$p_sql->bindValue(':custo_preco', $custo->getPreco());
$p_sql->bindValue(':custo_observacao', $custo->getObservacao());
$p_sql->bindValue(':colaborador_colaborador_id', $custo->getColaborador_id());
return ($p_sql->execute()) ? 'salvo' : false;
} catch (PDOException $e) {
return 'Erro, contate o suporte!Código:' . $e->getCode() . 'Mensagem:' . $e->getMessage();
}
}
public function Update(Post $custo) {
try {
$sql = "UPDATE tb_custo SET custo_descricao= :custo_descricao , custo_mes= :custo_mes , custo_local= :custo_local , custo_preco= :custo_preco , custo_observacao= :custo_observacao , colaborador_colaborador_id= :colaborador_colaborador_id WHERE custo_id= :custo_id ";
$p_sql = Conexao::getInstance()->prepare($sql);
$p_sql->bindValue(':custo_descricao', $custo->getDescricao());
$p_sql->bindValue(':custo_mes', $custo->getMes());
$p_sql->bindValue(':custo_local', $custo->getLocal());
$p_sql->bindValue(':custo_preco', $custo->getPreco());
$p_sql->bindValue(':custo_observacao', $custo->getObservacao());
$p_sql->bindValue(':colaborador_colaborador_id', $custo->getColaborador_id());
$p_sql->bindValue(':custo_id', $custo->getId());
return ($p_sql->execute()) ? 'salvo' : false;
} catch (PDOException $e) {
return 'Erro, contate o suporte!Código:' . $e->getCode() . 'Mensagem:' . $e->getMessage();
}
}
}
This structure is object oriented?
Is it, got doubt at some point? suspected that it was not pq?
– rray
Some adjustments are missing... but you have separated the model class from the operation class ... it is nice to go on this path. You need to see the Connection class. One thing not to put the connection class in the DAO class. put it as include at the beginning of your code.
– novic
I was watching on the net and saw other ways to assemble an OO structure , then I was doubtful in this my structure.
– Marlon Castro
It was worth the feedback Virgilio Novic
– Marlon Castro
@Marloncastro is missing some details, but, is on the way, could be improved with
namespace
if so prefer some, things I think cool implement, but, dude is cool.– novic
http://answall.com/questions/163660/namespace-com-php/163662#163662 also @Marloncastro
– novic
Thank you so much for your attention @Virgilionovic , I will study about all this :)
– Marlon Castro