Create another class or not? PHP/POO

Asked

Viewed 189 times

0

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

Recipe: https://pastebin.com/5DgxrhnD Expense: https://pastebin.com/LbfeCR2N Receitadespesa: https://pastebin.com/inWC65bq

  • 1

    You need to be more specific. What method is this? Who are the classes? What filter is this? What is the expected result?

  • It is a method that lists the revenues and expenses.. the filter is to know if some data were informed in the form to increment the sql query.

  • If expenses and receipts have common methods, you can create a class operacoes and make it inherited. (DRY) you avoid repeating the same blocks of code, and more agile is easier to maintain.

  • You can inherit from both classes?

  • Still very confusing. Have to [Edit] the question and add the codes?

  • I edited the question by adding 3 links.. Take a look at the links and read the question again I think that now with the links I can understand better.

Show 1 more comment

1 answer

2

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

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(){} 

}

Just a poorly made sketch to understand how it works ....

Here is also a cool example about this PHP data abstraction

Hugging

Following the request for assistance I am changing this response:

Eduardo is practically all set up just go in the Recipe class and where is "class Recipe {" put like this class Recipe extends Receitadespesas{" I would change the name of this class to Finance or any other Ministry. Cohesion is better. and in the Expenses class where it is "class Expense {" place "class Expense extends Receitadespesas {" ready the instances of the Revenue and expenses class will access the methods of the inherited class Receitadespesas

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 must ask me, how do I access the methods within Revenue and Expenses direct from the inherited class? Answer: In the same way as accessing an internal method from within the very class that defined it with the operator

$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 that is to indicate that this class is an abstraction of data or whatever is equal variables and methods in Income and Expenses will be put there and as I inherited in the model classes belongs to the class that the inherits. And therefore, since it is an abstraction of data, the class cannot be instantiated only inherited if I take the Abstract ai yes could be instantiated the part.

Also note that I used protected before setting the $notes variable and the set() and get() methods this indicates that these methods and variables can only be accessed for heirs of this class.

You can use : protected , public and private

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

protected: access to the method or variable only for the class that is heiress.

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

Another interesting aspect 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 serves to specify that both classes that inherit Wastes have to implement internally the setIdUsuario() class; this helps when you need to have this method implemented in different ways in various classes that inherit abstraction. That is in expenses could use the customer’s CPF and in revenues the customer’s identity, for example.

PS: in the case of the above example running will give error saying that a method has been defined as abstract:

Abstract setIdUsuario()

but it has not been implemented in the Income and Expense classes requiring you to implement them.

Finally I end by apologizing, I had no better functional example to insert. So I wrote down everything I have in mind.

If there are any more questions, don’t hesitate to call me

Hugging

  • Each class in a different file or everything in the same file?

  • 1

    Make each class in a different brother file to maintain cohesion and facilitate at the time of maintenance. And use a directory to modularize the project within it put the module interfaces, model class of the module where I have Set and Gets and Methods that I use in the interfaces and files of module constants.

  • Thanks for the reply friend... I’m still a little confused about this, but I’ll see some videos about abstraction and heritage... I tried to change my code here but it didn’t work out so well.

  • 1

    can change your question and post the code I guide how to mount if you want...

  • The codes are in the 3 links of the question. If you help me thank you from the heart!!!

Browser other questions tagged

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