Is my code object-oriented?

4

I have a code and would like to know if it is an object-oriented code. Follow the structure. I work with 3 files.

1st call save.php , follow 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 that 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 is where I make my 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();
        }
    }

   }

Is this structure object-oriented?

    
asked by anonymous 06.12.2016 / 18:42

1 answer

4

As far as you can object orientation in PHP , the Post class is pretty OOP , just is not anymore because it is not using inheritance, polymorphism, still good. There is encapsulation and some abstraction. The gain of having done this way seems close to zero, but I can not say that there is something wrong. An associative array would have been much simpler, which made me think that the gain could be considered negative.

DaoCusto is a little less. Everything that is static is less object-oriented, but it's a little bit. It would have been much simpler to create a simple function. It is forcing the use of a mechanism that does not fit there. It works, there are people who like to do so, but there is no gain, on the contrary.

The question is: what is the relevance of this? The goal is to follow a formula without understanding why or to do something that helps the code get better? OOP does not make the code look better by magic and there are cases that it can worsen what it's doing. It does not seem to be so much your case, when you needed to be pragmatic, it was at least in parts.

See: What is "Object Oriented" and what other methods?

Understand that object targeting is somewhat secondary to programming . In PHP even more .

    
06.12.2016 / 18:53