TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Help with class.... (http://www.talkphp.com/absolute-beginners/4153-help-class.html)

allworknoplay 04-17-2009 09:20 PM

Help with class....
 
Ok, I am working on a pagination class, I figured I will break up the "methods" and figure them out one at a time instead of trying to write a class with 10+ methods...so I am going to do one at a time, make sure I get the results, and eventually combine everything together.

But I am having a problem already, I keep getting a blank page.



Here's my class file.

Code:

<?

class pagination {

/* GENERATE LINKS ex: 1 2 3 4 5 6 7 8 */
public function getPageLinks(int $page_last,int $get_page) {

$this->pageLast = $page_last;
$this->getPage = $get_page;
       
for ($i=1; $i<=$this->pageLast; $i++) {

if(($this->getPage - 2) == $i) $display_pages[] = " <a href=\"$_SERVER[PHP_SELF]?p=$i\">$i</a>";
if(($this->getPage - 1) == $i) $display_pages[] = " <a href=\"$_SERVER[PHP_SELF]?p=$i\">$i</a>";
if($this->getPage == $i) $display_pages[] = "$i";
if(($this->getPage + 1) == $i) $display_pages[] = " <a href=\"$_SERVER[PHP_SELF]?p=$i\">$i</a>";
if(($this->getPage + 2) == $i) $display_pages[] = " <a href=\"$_SERVER[PHP_SELF]?p=$i\">$i</a>";

        }
               
return $this->displayPages = $display_pages;
                       
}
               
               

}###END CLASS
       
?>


Here's my HTML.

Code:

<?

include_once("pagination.php");

/* LETS SET SOME VARIABLES TO SIMULATE USER EXPERIENCE */
$page_limit = 25;
$get_page = 7;
       
/* ARRAY WILL SIMULATE DATABASE ENTRIES */
$array = array(1,2,3,4,5,6,7,8,9,10);
$totalrows = count($array);
$page_last = 10;

/* CREATE OBJECT */
$pagination = new pagination();

$display_links = $pagination->getPageLinks(int $page_last,int $get_page);
       
echo "implode(' ',$display_links)";

?>



Since the current page is set to 7. I am giving it the last page which is 10, and the current page 7.


So anyways, the output should be: 5 6 7 8 9

But instead it's just blank??

allworknoplay 04-17-2009 09:34 PM

Sorry, it's not blank, I was looking at the wrong browser.

I'm getting a parse error...


Parse error: syntax error, unexpected T_VARIABLE in /usr/local/apache/public/paginate.html on line 17

allworknoplay 04-17-2009 10:13 PM

Ok I got it working but I had to remove the INT from the function calls.

So in the HTML, I had to do this:

$display_links = $pagination->getPageLinks($page_last,$get_page);

Instead of:

$display_links = $pagination->getPageLinks(int $page_last, int $get_page);


And in the class, I had to use this:

public function getPageLinks($page_last, $get_page) {


Instead of this:

public function getPageLinks(int $page_last,int $get_page) {


But I don't understand, I thought it was proper coding to type hint the attributes?

Salathe 04-17-2009 10:23 PM

Quote:

Originally Posted by PHP_Manual
Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported.

Plus, you wouldn't want to be type hinting when calling a method anyway. Only in the function/method declaration. Using it the way that you did, the argument would need to have belonged to a class/interface called 'int'.

allworknoplay 04-17-2009 10:26 PM

Quote:

Originally Posted by Salathe (Post 23165)
Plus, you wouldn't want to be type hinting when calling a method anyway. Only in the function/method declaration. Using it the way that you did, the argument would need to have belonged to a class/interface called 'int'.

thanks Salathe...I "think" I understand...

Tanax 04-18-2009 12:41 PM

One thing to keep in mind though, is that every classvariable ($this->classvariable) should be declared in the beginning of the class.

Like such:
PHP Code:

class Pagination {

private 
$classvariable1;
private 
$classvariable2;
private 
$classvariable3;

// in your methods you can then access them like normal: $this->classvariable1 or 2 or 3 respectivly.



Note that you can use whichever scope you want right now, but I would strongly advice you not to use public classvariables.

Also note that it will work even if you don't declare them, however the PHP will work faster if you declare them, so just an advice.

allworknoplay 04-18-2009 02:03 PM

Quote:

Originally Posted by Tanax (Post 23182)
One thing to keep in mind though, is that every classvariable ($this->classvariable) should be declared in the beginning of the class.

Like such:
PHP Code:

class Pagination {

private 
$classvariable1;
private 
$classvariable2;
private 
$classvariable3;

// in your methods you can then access them like normal: $this->classvariable1 or 2 or 3 respectivly.



Note that you can use whichever scope you want right now, but I would strongly advice you not to use public classvariables.

Also note that it will work even if you don't declare them, however the PHP will work faster if you declare them, so just an advice.

Yes I am declaring all my properties to "private". I hope I can show you my class later today, it is almost done.

I think writing a pagination class script is a good way to learn OO. Much better than doing a "hello world" in OO....


All times are GMT. The time now is 06:35 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0