Is my code object-oriented?

Asked

Viewed 247 times

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?

  • 2

    Is it, got doubt at some point? suspected that it was not pq?

  • 1

    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.

  • I was watching on the net and saw other ways to assemble an OO structure , then I was doubtful in this my structure.

  • 1

    It was worth the feedback Virgilio Novic

  • 1

    @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.

  • http://answall.com/questions/163660/namespace-com-php/163662#163662 also @Marloncastro

  • Thank you so much for your attention @Virgilionovic , I will study about all this :)

Show 2 more comments

1 answer

6


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

DaoCusto is a little less. Everything that is static is less object oriented, but it is 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, some people like to do so, but there is no gain, on the contrary.

The question is, what is the relevance of this? Is the goal to follow a formula without really understanding why or to do something that helps the code get better? OOP doesn’t make the code magically better, and there are cases where it can make what it’s doing worse. It doesn’t seem to be so much your case, when you had to be pragmatic, as it was, at least in parts.

Behold: What is "Object-oriented" and what other methods?

Understand that object orientation is something secondary to programming. In PHP even more so.

  • Exactly , I did it in the little I learned watching on the net. But I was researching and I saw more in-depth things, and I wanted to know if this code can be considered OO , but thanks for the links , I will study them.

  • "and there are cases that it can make what it is doing worse" - In PHP I would say that is what happens most.

  • Object-oriented code seeks to repurpose behaviors by increasing the cohesion of the code that is written. If you have a DAO class that receives a concrete object, every time you implement a new one you will have to write a new class with virtually identical codes, because as Maniero said: there is no advantage in implementing it this way. An associative array passed to an abstraction that receives a generic and not a specific object would be more suitable and would help to remove this unnecessary granularity.Take as an example an abstraction advantageous to this example the Orms

Browser other questions tagged

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