Classes with operations extending another class

1

I have a class called cliente

class cliente {
    private $id;
    private $nome;

    function getId() {
        return $this->id;
    }

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

    }

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

And two other classes called selecionaCliente and insereCliente these two extend the cliente class:

<?php
require_once 'cliente.php';
class insereCliente extends cliente{
    private $pdo;           
    function __construct() {
    require_once 'conbdd.php';        
    $db = new conbdd;
    $this->pdo = $db->conectar();        
    }
    function selId(){
        try{
            $sel=$this->pdo->prepare("SELECT name FROM clientes WHERE 
            id=:id");
            $sel->bindValue(":id", $this->getId());
            $sel->execute();
            $temp=$sel->fetch();
            $this->setNome($temp['nome']);
            return TRUE;        
        } catch (PDOException $ex){
            echo $ex->getMessage();
            return FALSE;

        }        
} 
<?php
require_once 'cliente.php';
class selecionaCliente extends cliente{
    private $pdo;           
    function __construct() {
    require_once 'conbdd.php';        
    $db = new conbdd;
    $this->pdo = $db->conectar();        
    }
    function insNome(){
    try {
        $ins= $this->pdo->prepare("UPDATE clientes SET nome=:nome WHERE 
        id=:id");
        $ins->bindValue(":id", $this->getId());
        $ins->bindValue(":nome", $this->getNome());
        $ins->execute();
        return TRUE;            
    } catch (PDOException $ex) {
        echo $ex->getMessage();
        return FALSE;
    }        
  }        
}     

I want to validate and store client data within the cliente class through the class that will use the information

If I instantiate the two classes selecionaCliente and insereCliente and set an information in selecionaCliente will I be able to retrieve this information through the instance of class insereCliente , or does each instance create a different client? If so, how could I make all classes use the same instance of cliente ?

    
asked by anonymous 16.04.2018 / 14:54

1 answer

3

The question does not have many details but you can say that the premise is wrong.

Inheritance was created to extend something naturally, what you are doing is not an extension, so inheritance does not fit. If you do not meet the Liskov replacement principle do not make inheritance. Few things deserve inheritance. Extension should be add something to an existing classes, not create something different and give direct access to the class.

If the template is wrong any mechanism it uses is unsuitable.

Composition is almost always the most appropriate route. In this case I do not know how to turn this supposed inheritance into composition is the solution. Neither seems to be a case of composing.

You may actually need a trait for Cliente . If there is any composition the customer has the ability to select or insert the customer. But I think this is something more advanced, I probably could not use it properly.

If you're strictly following object-oriented, and I'm not saying that's the best solution, these classes should be just methods within Cliente . I could even talk in the interface, but the names do not fit this.

It seems that there is no cohesion in these classes, and they are so coupled that they should be one thing .

If you still follow the path of these classes, which I do not recommend, there would probably be a field in them that would receive the client to be manipulated. In the case of selecionaCliente , I can not imagine how this would be done because there does not seem to be a relationship with a single client, which indicates that every idea is wrong.

It is possible that these "classes" should be methods in an auxiliary class that handles clients. Anyway it goes by the simplest one until you learn to do the most complex.

My suggestion is to understand what you will use before using. It seems to me that there is a lack of understanding for what each thing serves in language.

By editing the question it is clear that there should be only 2 methods within the Cliente class or a DAOCliente class (which would receive the client per parameter) and no inheritance should occur. I'm not going to question if it should be like this in PHP and the other code problems, I already do this in several posts here on the site.

I can not help because the question does not help.

    
16.04.2018 / 15:03