Classes with operations extending another class

Asked

Viewed 82 times

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 class cliente:

<?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 customer data within the class cliente through the class that will use the information

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

  • selecionaCliente and insereCliente are strange names for classes. It seems that the problem is different. What would be the responsibility of each class?

  • the client class will validate and store the form information the insert class will do the Indsert in bdd and the selectClient will do the select in bdd and the two must take the already validated client class information

  • @Rodrigomarques The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how to do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points, and you will have to accept).

1 answer

3

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

Inheritance was created to extend something naturally, what you’re doing is not an extension, so the inheritance doesn’t fit. If you don’t meet the the principle of Liskov’s substitution do not make inheritance. Few things deserve inheritance. Extension should add something to an existing class, not create something different, and give direct access to the class.

If the model is wrong any mechanism you use is inadequate.

Almost always the composition is the most suitable way. In this case I do not even know if transforming this supposed inheritance into composition is the solution. Nor does it seem to be a case of composing.

It’s actually possible that you might be in need of a trait to the Cliente. If there is any composition is the customer have the ability to select or insert the customer. But I think this is something more advanced, probably could not use properly.

If you are to strictly follow object orientation, and I am not saying it is the best solution, these classes should be only methods within Cliente. I could even talk in interface, but by names not even fit this.

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

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

It is possible that these "classes" should be methods in an auxiliary class that manipulates customers. Anyway it goes by the simplest until learning 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 the language.

With the question edition it is clear that there should be only 2 methods within the class Cliente or a class DAOCliente (that would receive the account by parameter) and no inheritance should occur. I won’t even ask if it should be like this in PHP and the other code problems, I already do it in several posts here on the website.

I can’t help anymore because the question doesn’t help.

  • I’ll try to explain better:

  • the problem is that the client class gets more information as a phone address... and netbeans is complaining that it can only have 200 lines so I separated the Serts and select but the new class got big too so I was separating the Select from the Inserts and it occurred to me that each class will call a different customer class

  • 1

    Tell Netbeans to Wander. Do the right thing and don’t worry about it. I would turn off these meaningless "tips".

  • all right then,

  • When writing php code, I suggest trying to follow a standardization, which can be conferred here. Inheritance only becomes necessary, when it is meant to be part of the process of its scope (and usually has a rather direct relationship with it), it makes no sense to inherit an entity for a business model. I could transcribe you a simpler solution to the case, but first you need to understand how to use an entity. For example, you would have to create a "Manager" class to extend your entity "Client".

  • Within Manager, abstract these two classes and create only two methods: inserir(), listar(). In the builder you call the bank...

  • Instead of using require, you can use namespace; and with Composer install autoload... for your classes

Show 2 more comments

Browser other questions tagged

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