Pagination object oriented This a little pagination script that I am writing "Object Oriented" and I have no idea how to set the current page equal to total page, if the current page is greater.  I also would really like it if anyone can tell me how to improve my code.

class pagination extends Db_connection {

    public $per_page;
    public $current_page;
    public $sql;

    //put your code here
    function __construct($per = 3, $current = 1) {
        parent::__construct();
        $this-&gt;per_page = $per;
        $this-&gt;current_page = $current;
    }

    function execute_query($sql) {
        $this-&gt;sql = $sql;
        $q = $this-&gt;query($sql);
        return $q;
    }

    function total_pages() {
        $total = ceil($this-&gt;count_rows() / $this-&gt;per_page);
        return $total;
    }

    function current_page() {
        if (isset($_GET['page']) &amp;&amp; is_numeric($_GET['page'])) 
            $this-&gt;current_page = intval($_GET['page']);         

        return $this-&gt;current_page;
    }

    function offset() {
        $off = ($this-&gt;current_page() - 1) * $this-&gt;per_page;
        return $off;
    }
    public function previous_page(){
        //move to previous record by subtracting one into the current record
        return  $this-&gt;current_page - 1;
    }
    public function next_page(){
        //mvove to next record by incrementing the current page by one      
        return  $this-&gt;current_page + 1;

    }

    function count_rows() {
        $nums = mysqli_fetch_row($this-&gt;execute_query("select count(post_id) from Post"));
        return $nums[0];
    }

    function results() {
        $off = $this-&gt;offset();
        $per = $this-&gt;per_page;
        $query = $this-&gt;execute_query("Select * from Post limit $off, $this-&gt;per_page");
        while ($result = mysqli_fetch_array($query)) {
            $data[] = $result;
        }
        return $data;
    }
}
If you have to identify the main objects here, we could conclude that you have at least 3

-- The connection to the DataBase

-- The pagination object that knows how to paginate a query

-- The query it self

so ie this is a possible solution:
    

abstract class DBConnection{
    //Logic 
    public function executeQuery(Query $query)
    {
        $sql = $query-&gt;getSql();

        //Logic and return results
    }
}

class Query{
    private $sql;
    private $limit;
    private $offset;

    //getters and setters for each private property ... getSql ... setSql ...

    public function __construct($sql)
    {
        $this-&gt;sql = $sql;
    }

    public function getSql()
    {
        return $this-&gt;sql += "offset $this-&gt;getOffset() limit $this-&gt;getLimit()";
    }
}

class Paginator
{
    private $currentPage;
    private $perPage;
    private $query;
    private $connection;

    //Getters and setters...

    public function __construct(Query $query, $currentPage = 0, $perPage = 10)
    {
        $this-&gt;query = $query;
        $this-&gt;connection = new DBConnection();
    }

    public function nextPage()
    {
        $this-&gt;currentPage++;
    }

    public function prevPage()
    {
        $this-&gt;currentPage--; //check if 0 not substract
    }

    public function execute()
    {
        $this-&gt;query-&gt;setOffset($this-&gt;currentPage * $this-&gt;perPage);
        $this-&gt;query-&gt;setLimit($this-&gt;perPage);
        return $this-&gt;connection-&gt;executeQuery($this-&gt;query);
    }
}