Page 1 of 1

My first pagination class

Posted: Thu Nov 18, 2004 5:39 pm
by bradles
Hi All,

I have written a pagination class but just need someone to check that I am doing this right. My aim is to set up the class so that I can create a new pagination object and give it variables in the same line. So I made a class called pagination with a function inside it called pagination that takes some variables. Is this the right way to do it? It seems to work.

Code: Select all

<?
//Set up pagination Class
class pagination { 
	var $first;
	var $last;
	var $total;
	var $limit;
	var $page;
	var $numPages;
	
	function pagination($total, $limit, $page) {
		$this->total = (int) $total;
		$this->limit = max((int) $limit, 1);
		$this->page = (int) $page;
		$this->numPages = ceil($total / $limit);
		
		$this->page = max($page, 1); 
		$this->page = min($page, $this->numPages); 
		
		$this->first = ($page - 1) * $limit; 
		$this->last   = min((int)$this->first+$this->limit, $this->total);
	}
}
?>

I create it and pass some variables to it by using the following code:
$pagination = new pagination(12,6,1);
echo $pagination->numPages;

The echo was just to check that it was working ok.
Brad.

Posted: Thu Nov 18, 2004 7:29 pm
by rehfeld
at glance it looks ok to me.

usually classes are broken up into smaller functions/methods, and have more than just a constuctor, but if it meets the functionality you seek....

if you want more ideas, id highly suggest taking a good look at kettle_drums class, its very nice

http://www.redvodkajelly.com/index.php?v=code/3

Posted: Thu Nov 18, 2004 9:16 pm
by m3mn0n
Yeah, that class could easily just be a procedural function if all the functionality you need out of it can be accomplished within the constructor.

But it's a matter of preference so there isn't anything wrong with OOP in this case, I personally just think in cases like this it's a bit of overkill.

Posted: Thu Nov 18, 2004 9:37 pm
by John Cartwright
Go go a bit furthur with Sami's post I would just like to comment how this is a prime example of using OOP for all the wrong reasons. Althought there is nothing 'wrong' with using OOP in this fashion, as Sami said why not just give in to procedural :P

Posted: Thu Nov 18, 2004 10:28 pm
by bradles
Phenom wrote:I would just like to comment how this is a prime example of using OOP for all the wrong reasons. Althought there is nothing 'wrong' with using OOP in this fashion, as Sami said why not just give in to procedural :P
Not 100% sure of what you mean Phenom but that may be because I'm still learning. I am sure there will be things that I'm doing that may be done better in another way but at the moment I only have my knowledge base to work with...until this stuff gives me a brain hemorrhage :) then I'll have nothing.

For the time being, this class allows me to figure out which thumbnails need to be displayed on a page. I am going to put another function/method in it that displays what links need to be output to the browser so that I can easily call it at the top and bottom of my page.

Thank you all for your reply. I'm definately keen to hear any more advice you have.

Brad

Posted: Fri Nov 19, 2004 12:00 pm
by BDKR
I think it's fine as a class. Sure, it could be procedural, but as it is now, this could be considered core functionality that is extended with child classes.

I wrote a class that is doing some very similar things in the constructor. My eventual idea was to extend it to deal with different databases as there is some variance in the LIMIT syntax amongst different vendors.

Now of course, he may not even be deailing with a database, but that's why it's good they way it is. The core functionality is there and can extended to deal with any datasource he wants, be it a db, file, or even a directory. So just becuase it has only one method (the constructor) doesn't mean it's wrong.

That's how I see it.

Cheers...