Page 1 of 1
Multiple Objects or method to get all?
Posted: Thu Jun 24, 2010 10:12 am
by mikeeeeeeey
Hi,
Quick question regarding OOP Theory really.
Say you have a 'Page' class, which returns a page object that has methods such as getTitle(), getContent(), etc.
Code: Select all
class Page {
private $ID;
private $title;
private $content;
public function __construct($ID) {
// set ID
// query database to get the row etc.
}
public function getTitle() {
return isset($title) ? $title : false;
}
public function getContent() {
return isset($content) ? $content : false;
}
}
If you were to display a list or table of these pages - how would you do this in the PHP file which instantiates the object? Creating these objects in a loop doesn't seem right, and trying to write a method in the same class would not really fit the OO paradigm? I'm not sure how to go about it.
Code: Select all
// surely can't be right?
$page = array();
$page_numbers = array(1,2,3,4,5);
foreach ($page_number as $p) {
$page[] = new Page($p);
}
I can provide more info on request, just really wanting OOP to click.
Thanks in advance!
Re: Multiple Objects or method to get all?
Posted: Thu Jun 24, 2010 10:22 am
by Weirdan
There are at least two obvious options:
- Use static methods which would return collections of Pages - foreach (Page::listAll() as $page) ...
- Use separate finder/retriever/you name it object: $finder = new PageFinder; foreach ($finder->findOdd() as $page) ...
Re: Multiple Objects or method to get all?
Posted: Thu Jun 24, 2010 10:36 am
by mikeeeeeeey
Thanks for your reply Weirdan.
I would be tempted to use the static class however, if you are using the Page as an object i.e. a Page has a title, ID, content etc. would this not be the right way to approach?
The 2nd option you mentioned seems to be fit in with the flowchart I have in my mind.
Thanks again
Re: Multiple Objects or method to get all?
Posted: Thu Jun 24, 2010 10:52 am
by Weirdan
mikeeeeeeey wrote:I would be tempted to use the static class however, if you are using the Page as an object i.e. a Page has a title, ID, content etc. would this not be the right way to approach?
Conceptually, static methods are not bound to any particular object of the class, thus they might be viewed as operating on all instances of the class (like selecting odd pages). However, using static methods is not without it's drawbacks - it ties your code to the class name used. With non-static methods you're bound to interface - an object must implement called method, but there's no limitations on what class this object belongs to. For example you may use a subclassed object, or decorated object (google: Decorator pattern).
The 2nd option you mentioned seems to be fit in with the flowchart I have in my mind.
It's more flexible as well.
Another option is to use some container object - for example Pages could be belonging to a Book, and you'd invoke methods on the book to find some pages:
Code: Select all
foreach ($book->getEmptyPages() as $page) {
$page->write('this was once an empty page, but it\'s no longer is');
}
Re: Multiple Objects or method to get all?
Posted: Wed Jun 30, 2010 6:29 am
by josh
Static methods should only be used when there is no state. Pages have state (title, body). Ex. of good use for static method - a math function that multiples 2 numbers & returns them.
Rather than a page return a page (makes no sense? Would a car build a car in real life? Or would the factory build the car?). So you need some sort of "factory" object who's responsibility is birthing new objects. Normally I'll have like a Page_Mapper and then I can keep all my database queries separate from everything else, so when I need to tweak my queries I go straight to the mapper objects.