无法回显php类变量

use*_*958 0 php variables class

我想写一个php简单的分页类.这是我的代码:

<?php
class Pagination {
public $rowsPerPage;
public $currentPage;
public $totalPages;
public $totalRows;
public $firstElement;
public $lastElement;


     public function paginate()
        { 
            if(isset($_GET['page'])){
                $this->currentPage  = $_GET['page'];     
            }           

                         $this->totalPages =  $this->totalRows/$this->rowsPerPage  ;
             return $this->totalPages;  

              $this->firstElement = $this->currentPage-1)*$this->rowsPerPage+1;
              return $this->firstElement;                
        }
}

$totalRecords = 55;
$paginator = new Pagination();
$paginator->totalRows = $totalRecords;
$paginator->rowsPerPage = 5;
$paginator->paginate();
echo $paginator->totalPages;
echo $paginator->firstElement;  
?>
Run Code Online (Sandbox Code Playgroud)

我可以回显$ paginator-> totalPages; 但不是$ paginator-> firstElement.我做错了什么?谢谢

Roc*_*mat 8

你的paginate函数有两个return语句.

当代码命中时return $this->totalPages;,它将停止运行该功能.因此,该$this->firstElement = ...行永远不会运行.

  • 打败我! (2认同)
  • @ChrisB:我是忍者^^ (2认同)