-1

I'm doing a project with PHP OOP. But I'm getting error: Fatal error: Call to a member function prepare() on a non-object in ..../PhotoSection.php on line 12. The below is my source code.

class DatabaseAdapter for connect to database

<?php

class DatabaseAdapter {

    protected $_pdo;

    function connect() {
        if (!$this->_pdo) {
            try {
                $this->_pdo = new PDO("mysql:host=localhost;dbname=xxx", "xxx", "123456");
                $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $this->_pdo->exec("SET CHARACTER SET utf8");

                $this->_pdo->exec("set character_set_client='utf8'");
                $this->_pdo->exec("SET character_set_results='utf8'");
                $this->_pdo->exec("SET collation_connection='utf8_general_ci'");
            } catch (PDOException $e) {
                echo $e->getMessage();
                exit;
            }
        }
    }

    function disconnect() {
        $this->_pdo = null;
    }

}

class DatabaseBusiness

require 'DatabaseAdapter.php';

class DatabaseBusiness extends DatabaseAdapter {

    protected $_table = "";
    protected $_primaryKey = "";

    function __construct() {
        parent::connect();
    }

    function __destruct() {
        parent::disconnect();
    }

}

class PhotoSection

<?php

class PhotoSection extends DatabaseBusiness {

    function __construct() {
        $this->_table = "photo_section";
        $this->_primaryKey = "id";
    }

    function selectAll() {
        try {
            $stmt = $this->_pdo->prepare("select * from ?");
            $stmt->execute(array($this->_table));
        } catch (PDOException $ex) {
            echo $ex->getMessage();
            exit();
        }

        return $stmt->fetchAll();
    }

}

now I include to template to display data

<?php
include_once 'libraries/DatabaseBusiness.php';
include_once 'libraries/PhotoSection.php';
$photoSection = new PhotoSection();
$photos = $photoSection->selectAll();
?>

I've read the questions duplicate but I have declared and initialized _pdo in DatabaseAdapter and extends it, so I dont know why error. thanks for any helping

carboncrystal
  • 63
  • 1
  • 10

1 Answers1

0

you need to understand about the protected key word in php:

we use protected scope when we want to make your variable/function visible only in all classes that extend current class including its parent class

make this protected $_pdo; to global $_pdo; in your DatabaseAdapter file.

Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33
  • Yes. I use protected $_pdo in DatabaseAdapter. DatabaseBusiness extends DatabaseAdapter and PhotoSection extends DatabaseBusiness. So I think PhotoSection can use $_pdo, where's my wrong? – carboncrystal May 22 '15 at 10:39
  • i said please change that to global and see if that solves the probelm – Sourabh Kumar Sharma May 22 '15 at 10:41
  • I know your solution can solve my problem, but my mistake is don't call connect() function, so $_pdo is not initialized. anyway, thanks for helping. – carboncrystal May 22 '15 at 10:52