My first pagination class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bradles
Forum Commoner
Posts: 89
Joined: Wed Jun 30, 2004 10:40 pm

My first pagination class

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
bradles
Forum Commoner
Posts: 89
Joined: Wed Jun 30, 2004 10:40 pm

Post 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
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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...
Post Reply