Good practice is to create a set of designers

So, I am creating a PHP site to view my movie collection on my local network. This has been through several iterations, and now I think the object-oriented way. In the current state, I have several functions that receive movie information from the database. Therefore, whenever I need information for a movie, I have to call several functions to get all the information and pass it on to other functions in order to do what I want with it.

My idea for an object-oriented version is to execute all these getinfo functions in the constructor. So I just create a movie object, and all the information is easily accessible with $movieobj->title , etc.

I let him know and came up with this to test this:

 class movie{ public $tite = Null; function __construct($id, $conn){ //set title property $sql_select = $conn->prepare("SELECT title FROM movie.title WHERE `movieID` = {$id} LIMIT 1"); $sql_select->execute(); $sql_select->bind_result($val); $sql_select->fetch(); $this->title = $val; } 

}

This works the way I want, being able to get the name of the movie using:

 $movie = new movie(100,$db); echo $movie->title; 

But in practice, I would have some more similar pieces of code in the constructor to get other information for the movie.

Is it wrong to use a constructor? Should it be simpler and then use other methods to extract this information from the database? Having a complex constructor makes other code a lot easier, but is it bad practice, or can it cause problems that I don't see?

+6
source share
3 answers

In general, I personally would use only the constructor to set the base values ​​of the properties passed as arguments, such as a join, keeping the constructor code as simple as possible; and have all the real code in other methods, such as a fetch method (e.g. getMovie() ), to actually retrieve the database in your case, rather than directly access the movie property (which makes this property private or protected).

+2
source

It turns out that when you use Google to “query the database in the constructor”, you will find that this problem, even if you emphasize the complexity, has been redefined.

I find it bad practice to query the database in constructors.

There is no direct cost when you look at it at a "low level", people's discomfort is more general. Now it’s easy to ignore such non-specific objections, in practice, when you do this, you are likely to do everything you can, and is equally good compared to dividing the database query into a fetch method. In fact, you then need to write additional code, an additional line to retrieve the data.

What you get in such cases is on a different level than your specific small project in front of you: you force yourself to use a certain style that on average will do you more good, even if you have a (tiny) flaw in the short term.

The advantage is that when you get used to splitting database queries, you get flexibility and “order”: if the code changes later because you want to reload the data throughout the entire life cycle of the object, you need to reorganize what you already they wrote (including in all places where you created such an object, and if others used your code as a module, they should do it too) or destroy / recreate the object. If the fetch function is optional from the start, it will work no matter what!

So, your first proposal is suitable for those who write their personal hobby project on the weekend once a year, if you want or want to become a professional programmer for reasons beyond the scope of any particular project, you should go for something more maintained and streamlined long-term. Long-term projects that are programmed quickly and "dirty" in the enterprise, because they must be small in the end five years later, "monsters", because for the first two years, programmers believed that such short cuts are in order, because "this just a small project. "

As I see it, this is the real purpose of such agreements, to take care of what you do not see (the future).

+2
source

You can isolate database access from the constructor. In this case, your constructor should be used to set properties for the Movie class. Then my Movie class would have that Movie($title, $director, $year, ... ) signature Movie($title, $director, $year, ... ) . The Movie object will be a single row in my database. If I were making database calls again, I would have thought of putting this in a static function of the Movie class or as an external function, which I would name only once.

+1
source

Source: https://habr.com/ru/post/958710/


All Articles