hello everyone. i'm trying to make an mvc website based on this example http://anantgarg.com...amework-part-1/
Instead of using a common sql connection i have extend the model with a PDO. i made a class called PDOconnection and extends the model. When i load the page i'm getting this error message Fatal error: Call to a member function render() on a non-object in /Applications/MAMP/htdocs/project/library/controller.class.php on line 47
and when i made comment the model is loading the template. what my doing wrong? where is my code
controller.class.php
<?php
class Controller
{
protected $_model;
protected $_controller;
protected $_action;
protected $_template;
function __construct($model, $controller, $action)
{
$this->_controller = $controller;
$this->_action = $action;
$this->_model = $model;
//& = &
$this->$model = new $model;
$this->_template = new Template($controller,$action);
}
function set($name,$value)
{
$this->_template->set($name,$value);
}
function __destruct()
{
print_r($this->_template->render());
}
}
model.class.php
<?php
class Model extends PDOconnection {
protected $_model;
protected $_PDOconnection;
protected $_result;
private $_connection;
function __construct() {
$this->_PDOconnection = $this->PDO_connect(DB_HOST,DB_USER,DB_PASSWORD);
$this->_model = get_class($this);
$this->_table = strtolower($this->_model)."s";
}
function __destruct() {
}
}
pdoconnection.class.php
<?php
class PDOconnection
{
protected $_dbh;
protected $_result;
/** Connects to database **/
function PDO_connect($host, $username,$password)
{
try
{
$this->_dbh = new PDO($host, $username, $password);
$this->_dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
print_r($this->_dbh);
}
catch (PDOException $e)
{
/**
* If any error occur will trying to connect to the database it will store it on the errorlog file.
*
**/
error_log("Database is down!".date("d-F-Y h:i:s a", time()), 0);
die();
}
return $this->_dbh;
}
/** Disconnects from database **/
function disconnect()
{
$this->_dbh = null;
}
}
template.class.php
<?php
class Template {
protected $variables = array();
protected $_controller;
protected $_action;
function __construct($controller,$action) {
$this->_controller = $controller;
$this->_action = $action;
}
/** Set Variables **/
function set($name,$value) {
$this->variables[$name] = $value;
}
/** Display Template **/
function render() {
extract($this->variables);
if (file_exists(ROOT . DS . 'application' . DS . 'views' . DS . $this->_controller . DS . 'header.php')) {
include (ROOT . DS . 'application' . DS . 'views' . DS . $this->_controller . DS . 'header.php');
} else {
include (ROOT . DS . 'application' . DS . 'views' . DS . 'header.php');
}
include (ROOT . DS . 'application' . DS . 'views' . DS . $this->_controller . DS . $this->_action . '.php');
if (file_exists(ROOT . DS . 'application' . DS . 'views' . DS . $this->_controller . DS . 'footer.php')) {
include (ROOT . DS . 'application' . DS . 'views' . DS . $this->_controller . DS . 'footer.php');
} else {
include (ROOT . DS . 'application' . DS . 'views' . DS . 'footer.php');
}
}
}