Hey, I have been working on a class to use for pagination and I ran into a problem. I have a function that sets the variables that I will need, then I access them in the other funtions. For some reason one of my functions can't access any of the variables from the class, but the other functions can still access the variables.

Here is the code:
File: pagination.inc.php

<?php

class Pagination{
	function setUp($num = '',$numRows,$pageNum =  ''){
		// The number of rows per page
		if($num != ''){
			$this->num = $num;
		}else{
			$this->num = 25;
		}
		// Get the number of rows in the table
		$this->numRows = $numRows;
		// Divide number of rows, by number per page and round up to get the number of pages we need
		$this->numPages = ceil($this->numRows / $this->num);
		if($pageNum != ''){ // If the page is set it will be that page.
			if($pageNum > $this->numPages){
				$this->pageNum = $this->numPages;
			}elseif($pageNum < 1){
				$this->pageNum = '1';
			}else{
				$this->pageNum = $pageNum;
			}
		}else{ // else it will be the first page
			$this->pageNum = '1';
		}
		// The number of rows already shown, will be used for LIMIT in query
		$this->shown = ($this->pageNum - 1) * $this->num;
	}
	
	function pageNav(){
		$prev = $this->pageNum-1;
		$next = $this->pageNum+1;
		$this->pageList = "Pages: &nbsp;";
		if($this->pageNum != '1'){
			$this->pageList .= "<a href='?pageNum=1'>First</a> | ";
			$this->pageList .= "<a href='?pageNum=$prev'>Prev</a> | ";
		}else{
			$this->pageList .= "First | ";
			$this->pageList .= "Prev | ";
		}
		for($i = 1; $i <=$this->numPages; $i++){
			$this->pageList .= "<a href='?pageNum=$i'>$i</a> | ";
		}
		if($this->pageNum != $this->numPages){
			$this->pageList .= "<a href='?pageNum=$next'>Next</a> | ";
			$this->pageList .= "<a href='?pageNum=$this->numPages'>Last</a>";
		}else{
			$this->pageList .= "Next | ";
			$this->pageList .= "Last";
		}
		return $this->pageList;
	}
	
	function limitStatement(){
		return "LIMIT $this->shown,$this->num";
	}
}

?>

The limitStatement() function is the one that cannot access any variables. Here are the errors I get:

Notice: Undefined property: Pagination::$shown in pagination.inc.php on line 55

Notice: Undefined property: Pagination::$num in pagination.inc.php on line 55

I have even tried defining them as globals within the function, and that doesn't work either. I tried to define them as variables in the beginning of the class, and that only worked when I assigned a value to the variables.

In the past I have had problems with using reserved words for variables, so I changed the function name to see if that was it, and it did not fix the problem.

Any help that you can give me would be useful.

Thank you,
Key

Member Avatar for rajarajan2017
return "LIMIT $this->shown,$this->num";

May be this statement is wrong.

It's not the statement that is wrong, if I put in

return $this->shown;

it throws the same error.

The reason I put in the LIMIT is because it will be put into a query later on.

Member Avatar for rajarajan2017

declare the variable as global or public to share with all functions

Sample:

<?php 
class global_reference 
{ 
    public $val; 
    
    public function __construct () { 
        global $var; 
        $this->val = $var; 
    } 
    
    public function dump_it () 
    { 
        debug_zval_dump($this->val); 
    } 
    
    public function type_cast () 
    { 
        $this->val = (int) $this->val; 
    } 
} 
$var = "x"; 
$obj = new global_reference(); 
$obj->dump_it(); 
$obj->type_cast(); 
echo "after change "; 
$obj->dump_it(); 
echo "original $var\n"; 
?>

I have tried that as well, it only works if I do

var $val = "value";

it won't work if I just say it is a variable and define the value later on.
and if I put in...

public var $val = "value";

a parse error is thrown

if it helps, the page that I am using the class on has this code...

include("includes/pagination.inc.php");
$page = new Pagination;
$limit = $page->limitStatement();
echo $limit;

Hey, I have been working on a class to use for pagination and I ran into a problem. I have a function that sets the variables that I will need, then I access them in the other funtions. For some reason one of my functions can't access any of the variables from the class, but the other functions can still access the variables.

Here is the code:
File: pagination.inc.php

<?php

class Pagination{
	function setUp($num = '',$numRows,$pageNum =  ''){
		// The number of rows per page
		if($num != ''){
			$this->num = $num;
		}else{
			$this->num = 25;
		}
		// Get the number of rows in the table
		$this->numRows = $numRows;
		// Divide number of rows, by number per page and round up to get the number of pages we need
		$this->numPages = ceil($this->numRows / $this->num);
		if($pageNum != ''){ // If the page is set it will be that page.
			if($pageNum > $this->numPages){
				$this->pageNum = $this->numPages;
			}elseif($pageNum < 1){
				$this->pageNum = '1';
			}else{
				$this->pageNum = $pageNum;
			}
		}else{ // else it will be the first page
			$this->pageNum = '1';
		}
		// The number of rows already shown, will be used for LIMIT in query
		$this->shown = ($this->pageNum - 1) * $this->num;
	}
	
	function pageNav(){
		$prev = $this->pageNum-1;
		$next = $this->pageNum+1;
		$this->pageList = "Pages: &nbsp;";
		if($this->pageNum != '1'){
			$this->pageList .= "<a href='?pageNum=1'>First</a> | ";
			$this->pageList .= "<a href='?pageNum=$prev'>Prev</a> | ";
		}else{
			$this->pageList .= "First | ";
			$this->pageList .= "Prev | ";
		}
		for($i = 1; $i <=$this->numPages; $i++){
			$this->pageList .= "<a href='?pageNum=$i'>$i</a> | ";
		}
		if($this->pageNum != $this->numPages){
			$this->pageList .= "<a href='?pageNum=$next'>Next</a> | ";
			$this->pageList .= "<a href='?pageNum=$this->numPages'>Last</a>";
		}else{
			$this->pageList .= "Next | ";
			$this->pageList .= "Last";
		}
		return $this->pageList;
	}
	
	function limitStatement(){
		return "LIMIT $this->shown,$this->num";
	}
}

?>

The limitStatement() function is the one that cannot access any variables. Here are the errors I get:

Notice: Undefined property: Pagination::$shown in pagination.inc.php on line 55

Notice: Undefined property: Pagination::$num in pagination.inc.php on line 55

I have even tried defining them as globals within the function, and that doesn't work either. I tried to define them as variables in the beginning of the class, and that only worked when I assigned a value to the variables.

In the past I have had problems with using reserved words for variables, so I changed the function name to see if that was it, and it did not fix the problem.

Any help that you can give me would be useful.

Thank you,
Key

Hello,

I am not sure but may be because that variable having only local scope, so define them as global variable.

Hello,

I am not sure but may be because that variable having only local scope, so define them as global variable.

I have tried that...did not work

I have tried that...did not work

Can you please show to us, where you can put global variable..

It did not work so it is not in the code anymore, but I put it in like this:

function limitStatement(){
		global $shown;
		echo $shown;
	}

I tried to do it like this too...

function limitStatement(){
		global $this->shown;
		echo $shown;
	}

and it throws a fatal error:

Fatal error: Cannot re-assign $this in pagination.inc.php on line 56

Line 56 is where the global is (since you don't see the line numbers)

Any other thoughts?

this might shed some light

/**
 * Define MyClass
 */
class MyClass
{
    public $public = 'Public';
    protected $protected = 'Protected';
    private $private = 'Private';

    function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}

$obj = new MyClass();
echo $obj->public; // Works
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

(copyed it form php.net)

Hello,

Are you sure the others functions have access to your variables?
What do you have if you do:

<?php
include("includes/pagination.inc.php");
$page = new Pagination;
$page->setUp();
echo $page->pageNav();
$limit = $page->limitStatement();
echo $limit;
?>

I agree with rajarajan07, you should declare this as global or using class variable.

<?php

class Pagination{
    var $shown;
    var $num;
    var $numRows;
    var $pageNum;
    var $pageList;
    
    [...]
}
?>

You said it wasn't working, if you don't put value:

it won't work if I just say it is a variable and define the value later on.
and if I put in...

. But which error messages do you got?

this might shed some light

/**
 * Define MyClass
 */
class MyClass
{
    public $public = 'Public';
    protected $protected = 'Protected';
    private $private = 'Private';

    function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}

$obj = new MyClass();
echo $obj->public; // Works
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

(copyed it form php.net)

It works if I do it that way and define what the variable is in the beginning of the class, but it does not work if it is defined in another function.

Hello,

Are you sure the others functions have access to your variables?
What do you have if you do:

<?php
include("includes/pagination.inc.php");
$page = new Pagination;
$page->setUp();
echo $page->pageNav();
$limit = $page->limitStatement();
echo $limit;
?>

I agree with rajarajan07, you should declare this as global or using class variable.

<?php

class Pagination{
    var $shown;
    var $num;
    var $numRows;
    var $pageNum;
    var $pageList;
    
    [...]
}
?>

You said it wasn't working, if you don't put value: . But which error messages do you got?

If I put that code in for echoing the limit, it does not echo anything, it is just as if that statement was not in the code at all.

Never mind, I don't know what I did to fix it, but I just went in to look at the code and now it is working....weird. Thanks for everyone's help though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.