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
You need to be more specific. What method is this? Who are the classes? What filter is this? What is the expected result?
– Woss
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.
– Eduardo Henrique
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.– Papa Charlie
You can inherit from both classes?
– Eduardo Henrique
Still very confusing. Have to [Edit] the question and add the codes?
– Woss
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.
– Eduardo Henrique