MVC + DAO - Which way should I choose in PHP?

Asked

Viewed 444 times

3

In Java, in the model class I can create in the class funcionario the attribute Setor s, but PHP is not typed, as I could solve this association case?

After some searches here on the site, I saw that in PHP the MVC Model class is where methods are (CRUD...), but in Java there are only attributes, and these methods in the DAO class, this is due to the use of data persistence, or just a different way of using MVC?

  • I still have that doubt, too

2 answers

11


in Java, in the Template class I can create in the class funcionario the attribute Setor s, but PHP is not typed, as I could solve this association case?

The way people are taught OOP is based on statically typed languages so there are limitations when trying to use object orientation in dynamically typed languages. There is a form of programming that is based on the model of Smalltalk that many call object-oriented programming, but its creator recently admits that the model is message-oriented*.

PHP is still a language of script, your codes run for a fraction of a second or even for a few seconds, so they do things very fast, they are naturally microservices, so OO is not something that helps much in this type of environment.

So trying to play in PHP the way you do it in Java is weird, even if that’s what everyone else is doing. It’s just because someone said that anyone who doesn’t do it is the priest’s wife. If you want to use the same Java template and have something like PHP use Hack, which is what PHP wants to be and is maintained by highly qualified people to make it work.

Not even Java calls this what it is referring to as an attribute, only in UML that the term is used officially. In languages this is called a field. And in PHP you can’t have a type.

An important detail: many methodologies preach that should not be used getters/setters, I think it’s an exaggeration, but it makes some sense, the way people use it is wrong. Some questions to reflect:

One of the things you can do in a Setter in PHP is to check if the data is of a certain type. But PHP already allows you to put the type in the parameter and the interpreter can check for you. This makes more sense in a compiled language. This is nowhere near static typing that Hack has, that’s all hinting type that has some advantage.

In fact, classes in the way it is built, to use as contract, only makes sense in compiled and statically typed language.

One way that some people use, and I find it very plausible, in dynamic typing languages, although I am philosophically against it is the use of Hungarian notation. So all variables, and even methods, could have in their name indicating what their type is, which is a documentation that is hard to ignore, even if it slightly pollutes the name. But again, if this is a concern, it goes to Hack, or Java, or another language that uses the best of both worlds, like C#.

That’s a shallow attempt to avoid the gettier problem that rules around, which does not mean that this is the knowledge that he speaks.


* OOP is not message based

Tudo é objeto se comunicando através de mensagens X Abstração, herança e polimorfismo

if I wanted the name of the sector in which the employee is related, it would be $f = new Employee(); $f->getSetor->getName();

Responding comment, not exactly, would be more like:

$f->getSetor()->getNome();

I put in the Github for future reference.

This is a way of doing, assuming that sector is an object that has a method that takes its name. Of course it could ignore the getter and directly access the field if make it public.

after some searches here on the site, I saw that in PHP the MVC Model class is where the methods are (crud...), but in Java there are only the attributes, and these methods in the DAO class, this is due to the use of data persistence, or just a different way of using MVC?

There are several ways to view the MVC. Classes (plural) that represent a model often have methods that do all the operations that the object can do, or it can have only methods that are business rules (it has a DAO), or it can be completely anemic and only have state, or you can make a compromise if you make a lot of sense.

People do it differently for two main reasons:

  • they don’t know what they’re doing and just copy a model they saw somewhere, so it doesn’t matter, it’s already a mistake the motivation of using
  • they know what they’re doing and they choose a path because it makes more sense for them to work like this, either because the team works like this, or because the problem it handles works better.

There are methodologies that preach one thing or another. It is impossible to say what is more certain, despite following a philosophy. Some frameworks require doing a certain way. For example: purer OOP uses the way to have everything on it; DDD He preaches everything very separately, and there’s the middle that is usually more pragmatic.

In very organized environments I prefer the DAO or something similar separately. In poorly organized environments, which is not the same as messy, just a form of little ceremony, which is what languages like PHP have always preached, so you do what’s simpler and you can put together if it’s something specific. To tell the truth one of the great advantages that PHP has and people are throwing in the trash programming as if it were Java is generalize solutions and even need to take care of the recording in a specific way.

Java doesn’t help much, although it can, but even more bureaucratic languages like C# are encouraging generalization with reflection and more recently with code generation, can have the best of both worlds.

People don’t do it differently because they don’t understand what they’re doing, just following cake recipes found in books, or worse, blogs and videos on the internet, or worse posts isolated by in forums, other sites and pasmem, in Whastsapp! This is what is called "Miojo tip"*, is fast, solve your problem in 3 minutes, but then it makes you hungry and causes you health problem! And I know that no one will read everything you have linked to form your own opinion :P


*Noodles is Nissin’s trademark and has nothing to do with it. The term "Noodle Tip" is my friend’s invention John Benito "Excel man" Savastano.

  • Thanks, it took me a lot of doubt about this part of the DAO, and it’s really like you said "People don’t do it differently because they don’t understand what they’re doing," that’s/was my case regarding this.

  • 1

    Yes, @Leandro normal when learning, should not be normal when experienced :)

1

First question: in Java, in the Model class I can create in the Class the attribute Sector s, but PHP is not typed, as I could solve this association case?

You create the attribute the same way:

class Funcionario{
    private $setor;
    //...getters e setters
}

Obviously, nothing will force you to put in sector an object of the type Sector, but if you are minimally organized you will know that this variable was created for this. Another option is to use some kind of validation in the setters, if guaranteeing the type is so critical for you.

Second doubt: after some searches here on the site, I saw that in PHP a MVC Model class is where methods are (crud...), but in Java are only attributes, and these methods in the DAO class, this is due use of data persistence, or just a different way of using the MVC?

This is more organization of who is writing the code than general rule. By definition in Model is where the business rules of the application would be, but as often what we see on the internet are simpler and more didactic examples, you see this kind of thing. Another issue is that most PHP frameworks do not use DAO for persistence, but the Activerecord model, where the persistent entity class itself contains CRUD methods. This ends up creating these situations where simple systems end up creating Models that just pass on CRUD, rather than actually having some business logic.

  • Only one question regarding the first answer, if I wanted the name of the sector in which the employee is related, would be $f = new employee(); $f->getSetor->getName(); ?

Browser other questions tagged

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