OOP class start question using classes

I am trying to replace a site written procedurally with a good set of classes as a training exercise.

So far, I have created a record class that basically contains one row in the main database table.

I also created a loader class that can:

 loadAllFromUser($username) loadAllFromDate($date) loadAllFromGame($game) 

These methods capture all valid rows from the database, pack each row into a record, and insert all records into an array.

But what if I just want to work with one record? I took a shot at this and ended up with code that was almost identical to my procedural original.

I was also not sure where this record would go. Does my loader class have a protected write property?

I am a bit confused.

EDIT - also, where would I put something like an HTML template to output a post to a site? Does this happen in the write class, in the loader, or in the third class?

+4
source share
4 answers

I recommend learning something like Doctrine to abstract your stuff with a db-to-object, other than for educational purposes.

Nevertheless, there are many ways to model this type of thing, but in general it seems that the libraries (homegrown or not) that process it tend to move to a high level:

  • A class representing an object that maps to db
  • A class that represents the way this object maps to db
  • A class representing methods for extracting objects from db

Think about the different tasks that you need to complete, and try to encapsulate them cleanly. Demeter's law is useful to keep in mind, but not too bogged down, trying everything in the object-oriented theory of design this moment - it can be much more useful to think, design, code and see where weaknesses lie in your projects.

For your work with a single record, but without duplicating code problems, it is possible that something like that your loadAllFromUser methods are actually methods that call a private method that takes (for example) a parameter that is the number of records to write, where if this parameter is zero, it retrieves all records.

You can take this step further and implement __call in your loader class. Assuming that he can know or find out about the fields you want to load, you can build parameters for a function that performs software loading - look at the common parts of your functions, see what is different, and see if you can find a way to turn these different parts in functional parameters or something else that avoids repetition.

MVC is worth reading on your second question. At the very least, I probably want to have this in a separate class that expects a report to be rendered for rendering. The record probably should not care about how it is presented in html, what the markup for the record does not care about how the record is written. In general, you probably want to try to make things as autonomous as possible.

It’s not easy to get used to, and most of the β€œgood” ones with this design are a matter of practice. For the actual functionality, tests can help a lot - let's say you write your loader class, and you know that if you call loadAllFromUser($me) , you should get an array of three specific records with your data set (even if it uses a data set for testing), if you have something that you can run that will call it on your bootloader and check the correct results, this can help you find out that your code is at least right in terms of behavior, if not design - and when you change the design, you You can make sure that he is still behaving correctly. PHPUnit is apparently the most popular tool for this in php-land.

Hope this points to a useful group of directions, not just confusion :) Good luck and goddess.

+2
source

You can encapsulate the unique parts of loadAllFrom... and loadOneFrom... in utility methods:

 private function loadAll($tableName) { // fetch all records from tableName } private function loadOne($tableName) { // fetch one record from tableName } 

and then you will not see so much duplication:

 public function loadAllFromUser() { return $this->loadAll("user"); } public function loadOneFromUser() { return $this->loadOne("user"); } 

If you like, you can break it down as follows:

 private function load($tableName, $all = true) { // return all or one record from tableName // default is all } 

you can replace all these methods with calls, for example:

 $allUsers = $loader->load("users"); $date = $loader->load("date", false); 
+2
source

You can check the arguments included in your method and decide from there.

  $args = func_get_args(); if(count($args) > 1) { //do something } else // do something else 

Something simple, it might work. Or you can make two different methods inside your class to handle each type of request, like the @karim example. Which is best for what you would like to do.

I hope I understand what you are asking.

To answer your edit:

Usually you need to create a view class. This will be responsible for processing the HTML output. Good practice is to keep them separate. The best way to do this is to inject your data class object directly into the view class, for example:

 class HTMLview { private $data; public function __construct(Loader $_data) { $this->data = $_data; } } 

And then continue with the exit now that this class contains your processed database information.

+1
source

It is entirely possible and plausible that your record class may have a utility method associated with it that knows how to load one record, given that you provide it with some of the identifying information (for example, its identifier, for example).

The sample I used is that the object can know how to load itself, and also provides static methods to perform the "loadAll" actions, returning an array of these objects to the calling code.

So, I understand a lot of this with the small open source web application that I am developing too, I wrote most of this in the crunch procedurally because it was, as I knew, to get the job done (hehe, yes) applications as soon as possible - and now I go back and implement the powerful OOP and MVC architecture.

0
source

All Articles