Hallo mensen,
Ik ben bezig met een nieuwe website. Het lukt me niet om in het bestand 'libs/Category.php' een PDO query uit te voeren omdat ik een undefined property aanroep (Bootstrap::$db). Ik snap niet wat ik fout doe. Heb de betreffende bestanden (gedeeltelijk) toegevoegd.
Weet iemand hoe ik in libs/Category.php toegang kan krijgen tot $db? Of althans een parent::__construct() kan uitvoeren waardoor ik de beschikking krijg tot een db-connectie? Ik kom er niet uit....
Owja, het is geen MVC (ondanks de Model.php).
EDIT:
Dit is de error die ik krijg:
Notice: Undefined property: Bootstrap::$db in E:\root\2012\Website\libs\Category.php on line 30
Fatal error: Call to a member function select() on a non-object in E:\root\2012\Website\libs\Category.php on line 30
index.php:
----------
<?php
require 'config.php';
// Include libraries
function autoLoadLibs($class) {
require LIBS . $class .".php";
}
spl_autoload_register('autoLoadLibs');
include 'classes/Model.php';
// Start page
$app = new Bootstrap();
?>
libs/Category.php:
-----------------
<?php
/* ===== Category ===== */
class Category extends Model {
public $db = "";
public function __construct() {
parent::__construct();
}
public function getCatIdFromCatName($catName) {
$sql = "SELECT cat_id FROM category WHERE cat_name = :cat_name";
return $this->db->select($sql, array('cat_name' => $catName));
}
?>
classes/Model.php:
------------------
<?php
class Model {
function __construct() {
$this->db = new Database(DB_TYPE, DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASS);
}
}
?>
libs/Bootstrap.php:
-------------------
<?php
class Bootstrap {
function __construct() {
// Get URL pieces
$url = Functions::getUrlPieces();
// Include homepage if no specific page was requested
if (empty($url[0])) {
require 'pages/index.php';
return false;
}
// If specific page was requested, check whether that page exists
// If the page does exist, include the page
// If the page does not exist, show errorpage.
$file = 'pages/' . $url[0] . '.php';
if (file_exists($file)) {
require $file;
} else {
require 'pages/404.php';
}
}
}
?>
2.198 views