Jump to content

Fatal error: Call to a member function render() on a non-object


giwrgos88

Recommended Posts

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');
}
}

}

Just to make sure your $this->_template object has instantiated in your constructor method. I am not sure about the framework you used, usually PHP framework don't encourage overriding the default constructor method. Check your manual or simply echo a string in your constructor to see if the processor run that method.

=]

It seems likely that the controller.class.php hasn't got visibility of the template.class.php. either the template class file isn't being included in the preloader, or the script that you are loading it from could be loading it in the wrong order.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.