我必须做一个分页脚本,我必须特别注意性能,安全性和算法。我想出了以下代码:
<?php
class Paginador{
public $current_page; // current page
public $total_pages; // total
public $boundaries = 1; // pages to link in the beggining and in the end
public $around = 1; // pages to link before and after the current
public $pageArray = array(); // array com toda a informação a paginar
function __construct( $current_page, $total_pages, $boundaries = 1, $around = 1){
$this->current_page = $current_page;
$this->total_pages = $total_pages;
$this->boundaries = $boundaries;
$this->around = $around;
}
function __destruct(){
$this->current_page = 0;
$this->total_pages = 0;
$this->boundaries = 0;
$this->around = 0;
}
function setCurrentPage( $newValue ){
$this->current_page = $newValue;
return $this;
}
function setTotalPages( $newValue ){
$this->total_pages = $newValue;
return $this;
}
function setBoundaries( $newValue ){
$this->boundaries = $newValue;
return $this;
}
function setAround( $newValue ){
$this->around = $newValue;
return $this;
}
function generateLeftBoundaries(){
if ( $this->boundaries + $this->around > $this->current_page ) {
$this->pageArray = array_merge( $this->pageArray, $this->populateArray( 1 , $this->current_page ) );
} else {
if ( $this->boundaries > 0 ) {
$this->pageArray = array_merge( $this->pageArray, $this->populateArray( 1, 1 + ( $this->boundaries - 1 ) ) );
}
if ( $this->around > 0 ) {
$this->pageArray = array_merge( $this->pageArray, $this->populateArray( $this->current_page - $this->around, $this->current_page ) );
}
}
}
function generateRightBoundaries() {
if ( $this->boundaries + $this->around + $this->current_page > $this->total_pages ) {
$this->pageArray = array_merge( $this->pageArray, $this->populateArray( $this->current_page, $this->total_pages ) );
} else {
if ( $this->around > 0 ) {
$this->pageArray = array_merge( $this->pageArray, $this->populateArray( $this->current_page, $this->current_page + $this->around ) );
}
if ( $this->boundaries > 0 ) {
$this->pageArray = array_merge( $this->pageArray, $this->populateArray( $this->total_pages - ( $this->boundaries - 1 ), $this->total_pages ) );
}
}
}
function populateArray( $start, $end ) {
$output = array();
for ( $i = $start; $i <= $end; $i++ ) {
$output[] = $i;
}
return $output;
}
function paginate(){
//create the structure
$this->pageArray = array();
$this->generateLeftBoundaries();
$this->pageArray[] = $this->current_page;
$this->generateRightBoundaries();
$this->pageArray[] = $this->total_pages;
$this->pageArray = array_values( array_unique( $this->pageArray ) );
//now print the pagination array
$pages = $this->pageArray;
$items = count( $pages );
for ( $i = 0; $i < $items; $i++ ) {
$current_page = $pages[ $i ];
echo $current_page . ' ';
if ( ( $i + 1 ) < $items
&& $pages[ $i + 1 ] != ( $current_page + 1 ) ) {
echo '...';
}
}
}
}
?>
此代码目前按预期工作,我的问题是关于性能,易读性和安全性。如何提高此代码的效率,使其更易于理解。如果你能说出为什么它会很棒的原因。 Thx提前!
答案 0 :(得分:2)
对于某些性能和安全性,请设置您的方法的可见性。例如:
public function setCurrentPage( $newValue ){
$this->current_page = $newValue;
return $this;
}
或者
private function setCurrentPage( $newValue ){
$this->current_page = $newValue;
return $this;
}
如果您使用套餐,则受到保护。
您可以像在其他所有现代编程语言中一样检查变量的类型:
function setBoundaries( $newValue ){
$this->boundaries = $newValue;
return $this;
}
您可以在php http://www.php.net/manual/en/language.oop5.typehinting.php
中详细了解类型提示在您的课程中使用封装:https://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 所以让你变量变得私密。
并且不要退还$ this;在你的设置者。我真的不知道你为什么要那样。