0

I have a PDO database connection class which I am using throughout my site.

pdoConnect.php

class pdoConnect {
    private $host = "localhost";
    private $db = "database";
    private $userSelect = "user";
    private $passSelect = "XXXXX";
    protected $selectInstance = true;
    protected $options = [
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    ];

    function dsn() {
        return 'mysql:host=' . $this->host . ';dbname=' . $this->db;
    }

    function connectSelect() {
        if ($this->selectInstance === true) {
            $dsn = $this->dsn();
            $this->selectConnect = new PDO($dsn, $this->userSelect, $this->passSelect, 
                                           $this->options);
        }
        return $this->selectConnect;
    }
}

pagination.php

class pagination {
    var $php_self;
    var $rows_per_page = 10; //Number of records to display per page
    var $total_rows = 0; //Total number of rows returned by the query
    var $links_per_page = 5; //Number of links to display per page
    var $append = ""; //Parameters to append to pagination links
    var $sql = "";
    var $debug = false;
    var $conn = false;
    var $page = 1;
    var $max_pages = 0;
    var $offset = 0;
    var $multi = ""; //Use if multiple pagination instances are needed

    function pagination($connection, $sql, $rows_per_page = 10, 
                        $links_per_page = 5, $append = "", $multi = "") {

        $this->conn = $connection;
        $this->sql = $sql;
        $this->rows_per_page = (int)$rows_per_page;
        if (intval($links_per_page ) > 0) {
            $this->links_per_page = (int)$links_per_page;
        } else {
            $this->links_per_page = 5;
        }

        $this->append = $append;
        $this->multi = $multi;

        $this->php_self = htmlspecialchars($_SERVER['PHP_SELF'] );
        if (isset($_GET['page'] )) {
            $this->page = intval($_GET['page'] );
        }
    }

    function paginate(){

            if ($this->sql != null) {
                $all_rs = $this->conn->query($this->sql); //this is line 76
                $this->total_rows = $all_rs->rowCount();
            }

            ....
    }
}

It works fine everywhere else, but, if I instantiate it then pass it as an argument to another class the object is not accessible.

require_once '/classes/pdoConnect.php';
$db = new pdoConnect();
$pdo = $db->connectSelect();

require_once '/classes/pagination.php';
$pages = new pagination( $pdo, $sql, $page_amount, 8, null );

Then I get...

Fatal error: Call to a member function query() on a non-object in /classes/pagination.php on line 76

But the PDO connection object exists!

var_dump($pdo);
object(PDO)#1 (0) { }

The only thing I can think of, is that I have extended the pdoConnect class to a different class in order to gain access to the $selectInstance property. So in my IDE the pdoConnect class says is Overridden. Can someone please tell me what's going on here?

4
  • 2
    you should probably include the code inside pagination() where query() is called. Commented May 15, 2015 at 20:29
  • as castis says you need to include the pdoConnect in your pagination class file. That is how PHP is designed. Commented May 15, 2015 at 20:41
  • try changing function pagination( to function __construct( in the pagination class. Commented May 15, 2015 at 20:45
  • @MikePurcell - seems that got rid of the error, but why does it need to be a constructor? It was working fine this way with mysqli (non class). Commented May 15, 2015 at 20:59

1 Answer 1

1

Try changing function pagination( to function __construct(

__construct and __destruct are special methods which are called (behind the scenes) anytime an object is instantiated or destroyed. If you want to pass arguments as part of object instantiation, then you need to do so via the __construct method. Another way would be to use accessors:

$pages = new pagination;

$pages->setConn($pdo);
$pages->setSql($sql);
$pages->setRowsPerPage($page_amount);
....

Your pagination function is not an accessor, it's a wrapper method meant to execute logic in the specific manner. Accessor is just a fancy term for getters and setters. Consider the following:

class Pagination
{
    $conn = false; // This is a member variable
}

Typically you do not want to allow outside code to set the value of a class's member variable directly, otherwise this breaks encapsulation. Rather you want to use an accessor, like this:

// somescript.php
$pager = new Pagination;

$pager->conn = $pdo; // Not a good practice

$pager->setConnection($pdo); // Setting the member variable through an accessor, preferred 
Sign up to request clarification or add additional context in comments.

6 Comments

You didn't post the mysqli code so not sure how to compare, only going off what you posted. Updated with other info.
Let me put it this way...as you can see in the pagination class, there already is a constructor which is where the connection originates ($this->conn = $connection;). The pagination method is using that.
pagination is NOT a constructor, __construct is THE constructor.
Well according to the documentation it is a constructor. A function becomes a constructor, when it has the same name as the class.
Which version of php are u running? According to php.net/__construct : As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn't affect non-namespaced classes. And you are obviously not using namespacing.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.