<?php
// namespace core;
// use core\view as View,
// core\error as Error;

/**
* Controller - base controller extends Model class
*/
class Controller extends Model{

	protected $_view;
	protected $_model;
	protected $_url;

	function __construct(){
		/* on run make an instance of the view class */
		$this->_view = new view();
		$this->_getUrl();
	}

	/**
	* Display an error page if nothing exists
	*/
	protected function _error($error) {
		require 'core/error.php';
		$this->_controller = new Error($error);
		$this->_controller->index();
		die;
	}

	/**
	* Set instance for current model class
	*/
	public function loadModel($name){
		/* all models should end with _model suffix */
		$modelpath = 'models/'.$name.'_model.php';
		// error_log($modelpath);
		/* check for model existents if note just ignore??? */
		if(file_exists($modelpath)){
			require $modelpath;
			// error_log($modelName);
			$modelName = ucwords($name).'_Model';
			$this->_model = new $modelName();
		}
	}

	/**
	* Set instance for current URL class from URL Path
	*/
	protected function _getUrl(){
		$url = isset($_GET['url']) ? rtrim($_GET['url'], '/') : NULL;
		$url = filter_var($url, FILTER_SANITIZE_URL);
		$this->_url = explode('/',$url);
	}
}