Beginning OOP question about classes using classes?

I recommend looking into using something like Doctrine for abstracting your db-to-object stuff, other than for learning purposes That said, there are many ways to model this type of thing, but in general it seems like the libraries (home-grown or not) that handle it tend to move towards having, at a high level: A class that represents an object that is mapped to the db A class that represents the way in which that object is mapped to the db A class that represents methods for retrieving objects from the db Think about the different tasks that need done, and try to encapsulate them cleanly The Law of Demeter is useful to keep in mind, but don't get too bogged down with trying to grok everything in object-oriented design theory right this moment -- it can be much more useful to think, design, code, and see where weaknesses in your designs lie yourself For your "work with one record, but without duplicating a bunch of code" problem, perhaps something like having your loadAllFromUser methods actually be methods that call a private method that takes (for instance) a parameter that is the number of records to be retrieved, where if that parameter is null it retrieves all the records You can take that a step further, and implement call on your loader class. Assuming it can know or find out about the fields that you want to load by, you can construct the parameters to a function that does the loading programatically -- look at the common parts of your functions, see what differs, and see if you can find a way to make those different parts into function parameters, or something else that allows you to avoid repetition MVC is worth reading up on wrt your second question. At the least, I would probably want to have that in a separate class that expects to be passed a record to render.

The record probably shouldn't care about how it's represented in html, the thing that makes markup for a record shouldn't care about how the record is gotten. In general, you probably want to try to make things as standalone as possible It's not an easy thing to get used to, and most of "getting good" at this sort of design is a matter of practice. For actual functionality, tests can help a lot -- say you're writing your loader class, and you know that if you call loadAllFromUser($me) that you should get an array of three specific records with your dataset (even if it's a dataset used for testing only), if you have something you can run which would call that on your loader and check for the right results, it can help you know that your code is at least right from the standpoint of behavior, if not from design -- and when you change the design you can ensure that it still behaves correctly PHPUnit seems to be the most popular tool for this in php-land Hopefully this points you in a useful group of directions instead of just being confusing :) Good luck, and godspeed.

I recommend looking into using something like Doctrine for abstracting your db-to-object stuff, other than for learning purposes. That said, there are many ways to model this type of thing, but in general it seems like the libraries (home-grown or not) that handle it tend to move towards having, at a high level: A class that represents an object that is mapped to the db A class that represents the way in which that object is mapped to the db A class that represents methods for retrieving objects from the db Think about the different tasks that need done, and try to encapsulate them cleanly. The Law of Demeter is useful to keep in mind, but don't get too bogged down with trying to grok everything in object-oriented design theory right this moment -- it can be much more useful to think, design, code, and see where weaknesses in your designs lie yourself.

For your "work with one record, but without duplicating a bunch of code" problem, perhaps something like having your loadAllFromUser methods actually be methods that call a private method that takes (for instance) a parameter that is the number of records to be retrieved, where if that parameter is null it retrieves all the records. You can take that a step further, and implement __call on your loader class. Assuming it can know or find out about the fields that you want to load by, you can construct the parameters to a function that does the loading programatically -- look at the common parts of your functions, see what differs, and see if you can find a way to make those different parts into function parameters, or something else that allows you to avoid repetition.

MVC is worth reading up on wrt your second question. At the least, I would probably want to have that in a separate class that expects to be passed a record to render. The record probably shouldn't care about how it's represented in html, the thing that makes markup for a record shouldn't care about how the record is gotten.In general, you probably want to try to make things as standalone as possible.

It's not an easy thing to get used to, and most of "getting good" at this sort of design is a matter of practice. For actual functionality, tests can help a lot -- say you're writing your loader class, and you know that if you call loadAllFromUser($me) that you should get an array of three specific records with your dataset (even if it's a dataset used for testing only), if you have something you can run which would call that on your loader and check for the right results, it can help you know that your code is at least right from the standpoint of behavior, if not from design -- and when you change the design you can ensure that it still behaves correctly. PHPUnit seems to be the most popular tool for this in php-land.

Hopefully this points you in a useful group of directions instead of just being confusing :) Good luck, and godspeed.

You can encapsulate the unique parts of loadAllFrom... and loadOneFrom... within utility methods: private function loadAll($tableName) { // fetch all records from tableName } private function loadOne($tableName) { // fetch one record from tableName } and then you won't 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 further like so: private function load($tableName, $all = true) { // return all or one record from tableName // default is all } you can then replace all of those methods with calls such as: $allUsers = $loader->load("users"); $date = $loader->load("date", false).

You could check the arguments coming into your method and decide from there. $args = func_get_args(); if(count($args) > 1) { //do something } else // do something else Something simple liek this could work. Or you could make two seperate methods inside your class for handling each type of request much like @karim's example.

Whichever works best for what you would like to do. Hopefully I understand what you are asking though. To answer your edit: Typically you will want to create a view class.

This will be responsible for handling the HTML output of the data. It is good practice to keep these separate. The best way to do this is by injecting your 'data class' object directly into the view class like such: class HTMLview { private $data; public function __construct(Loader $_data) { $this->data = $_data; } } And then continue with the output now that this class holds your processed database information.

1 Changing the behavior of the method depending on the amount of arguments passed to it, will make your API harder to read. Methods should do one thing. – Gordon Jul 16 at 7:58 Ah, valid point.

+1 – Headspin Jul 18 at 18:24.

It's entirely possible and plausible that your record class can have a utility method attached to itself that knows how to load a single record, given that you provide it a piece of identifying information (such as its ID, for example). The pattern I have been using is that an object can know how to load itself, and also provides static methods to perform "loadAll" actions, returning an array of those objects to the calling code. So, I'm going through a lot of this myself with a small open source web app I develop as well, I wrote most of it in a crunch procedurally because it's how I knew to make a working (heh, yeah) application in the shortest amount of time - and now I'm going back through and implementing heavy OOP and MVC architecture.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions