giwrgos88 Posted January 10, 2013 Share Posted January 10, 2013 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'); } } } Quote Link to comment Share on other sites More sharing options...
xenophobia Posted January 10, 2013 Share Posted January 10, 2013 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. =] Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted January 10, 2013 Share Posted January 10, 2013 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. Quote Link to comment Share on other sites More sharing options...
giwrgos88 Posted January 10, 2013 Author Share Posted January 10, 2013 hello guys!! i found the solution thank you very much!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.