Create another class or not? PHP / POO [closed]

0

My question is this: I have two tables and classes of finance, one for Despesas and another for Receitas . Both have some equal and different columns. I need to use a method (I've already created) that joins the two tables and makes a list based on a filter. The most correct would be to create a new ReceitaDespesa class or add the method to one of the two already existing classes? If it is more feasible to create a new class, should you also declare the variables for each attribute of each table?

Revenue: link Expense: link RevenueExpenses: link

    
asked by anonymous 04.07.2017 / 17:18

1 answer

2

Opa Eduardo because it does not create a data abstraction or an abstract class and uses an inheritance in both classes so all common methods are in the abstract class with access by inheritance and the methods belonging only to finances and expenses within its class model.

abstract class Financas{ 
    public function __construct(){}

    protected function Get(){}

    protected  function Set(){}

    //Gets e Sets e métodos compartilhados entre Despesas e Receitas

}

class Despesas extends Financas{

   public function __construct(){} 

}


class Receitas extends Financas{

   public function __construct(){} 

}

Only a poorly designed sketch to understand the operation ....

Here's a nice example about this PHP data abstraction

Embrace

Following the request in comment for help I'm changing this answer:

Eduardo is all practically assembled just go to the Recipe class and where is class Recipe {" put so Recipe extends Recipe Expenses {" I would change the name of this class to In the class Expenditure where class Expense {" place " class Expenditure extends RevenueExpenditure {" ready the instances of the Revenue class and expenses will access the inherited class methods RevenueExpenses

Assembly:

abstract class ReceitasDesesas{
    /* aqui deve colocar todos os métodos compartilhados entre as duas 
    classes abaixo, essa é uma típica classe abstrata poderia definir 
    abstract antes do class.*/  
}

class Receita extends ReceitaDespesas{
    // aqui fica como está
}

class Despesas extends ReceitaDespesas{
    // aqui fica como está
}

Now you should ask me, how to access methods within Revenue and Expenses direct from the inherited class? Answer: In the same way that you access an internal method from within the class itself that defined it with the

  

$ this   ;

abstract class ReceitasDesesas{
   protected $observacoes ;       

   /*Crie métodos dentro desta classe para colunas 
   do banco que tenham colunas iguais  em Despesas e Receitas exemplo set e 
   get entre outros métodos de formatação como por exemplo o método 
   observações copiado da sua classe*/

   /*Isso vai evitar métodos duplicados facilita e agiliza a manutenção do 
   sistema*/

   protected function setObservacoes($observacoes) {
      $this->observacoes = $observacoes;
   }


   abstract function setIdUsuario() { }


   protected function getObversacoes() {
      return $this->observacoes;
   }

}

class Receitas extends ReceitaDespesas{
    public function __construct(){
        $this->setObservacoes("Receita vinda da lavaJato caixa dois ...."); 
    }

   // Aqui criar métodos pertencentes apenas a receitas. 
}

class Despesas extends ReceitaDespesas{
   public function __construct(){
       $this->setObservacoes("Despesas com propina de deputado ....");
   }

   //Aqui criar métodos pertencentes apenas a despesas.
}

Notice the following I added up the abstract before the class this is to indicate that this class is a data abstraction ie everything that is equal variables and methods in Revenue and Expenses will be put there and as I inherited in the classes models belongs to the class that inherits it. So, as this is a data abstraction, the class can not be instantiated just inherited if I take the abstract out so yes it could be instantiated the part.

Also note that I used protected before setting the $ remarks variable and in the set () and get () methods this means that these methods and variables will only be accessible to heirs of this class.

You can use: protected, public, and private

  

public: access to the method or variable within the class itself or to any interface that instantiates it.

     

protected: method or variable access only for the class that inherits.

     

private: access to the method or variable only internally within the class that defined it.

Another interesting aspect that I can describe is the use of abstract methods notice that I put an example in our abstract class

  

abstract function setIdUsuario ();

and did not define the content of the method. This is to specify that both classes that inherit ExpendituresReceivers must implement internally the class setIdUsuario (); this helps when you need to have this method being implemented in different ways across several classes that inherit abstraction. That is, in expenses could use the customer's CPF and in revenue the identity of the customer, for example.

PS: in the case of the above example if executing will give error saying that a method was defined as abstract:

  

abstract setIdUser ()

but has not been implemented in the Revenue and Expense class, forcing you to implement them.

Finally I apologize, I had no better functional example to insert. So I wrote everything I have in mind.

If you have more questions, please do not hesitate to call me

Embrace

    
04.07.2017 / 20:34