Location: PHPKode > projects > ko > ko-framework-master/ko.php
<?php 
/**
* ko - a lightweight PHP framework using a MVC approach
*
* PHP Version 5
*
* The ko framework is released under a modified MIT license. A copy of this 
* license has been included below. You will also find a copy in the root of 
* this repository in a file named LICENSE. If you are unsure of anything in
* the license, please contact me at hide@address.com to clear anything up.
*
* Copyright (C) 2012 Maximillien Courchesne- Mackie (hide@address.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a 
* copy of this software and associated documentation files (the "Software"), 
* to deal in the Software without restriction, including without limitation 
* the rights to use, copy, modify, merge, publish, distribute, sublicense, 
* and/or sell copies of the Software, and to permit persons to whom the 
* Software is furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included 
* in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
* OTHER DEALINGS IN THE SOFTWARE.
*
* @author  Max Mackie <hide@address.com>
* @link    http://github.com/maxmackie/ko-framework
 */


/** --------------------------------------------------
 *  Database Connection Class
 * ---------------------------------------------------
 * Everytime an instance of the abstract model class is created,
 * it automatically creates a MySQL object using the predefined
 * default database connection settings defined in index.php.
 *
 * Instances of this class need not (and should not) be created
 * by the user -- let the framework handle it.
 */
class MySQL
{		
	var $connection;
	
	public function __construct($username, $password, $hostname)
	{
		$connection = mysql_connect("localhost","peter","abc123");
		if (!$connection) {
			throw new Exception("Cannot connect to MySQL server");
		}
	}

	public function __destruct()
	{
		mysql_close($connection);
	}

	public function set_database($database)
	{
		if (!mysql_select_db($database, $connection)) {
			throw new Exception("Cannot connect to MySQL database");
		}
	}

	public function query($query)
	{
		$array = array();
		$result = mysql_query($query, $connection);
		$num_rows = mysql_num_rows($result);
		for ($i = 0; $i < $num_rows; $i++) {
			$array[i] = mysql_fetch_row($result);
		}
		return $array;
	}
}


/** ------------------------------------------------------
 *  Abstract Controller
 * -------------------------------------------------------
 * Every controller must extend this class to function properly and
 * reap the benefits of the framework. The abstract controller has
 * functions to easily load models, views and even other controllers.
 */
abstract class ko_controller 
{
	public function __construct()
	{
		$this->index();
	}

	public function index()
	{
		echo "The controller you made is missing an index() function";
	}

	public function load_view($view_name)
	{
		$path = KO_VIEW_PATH . $view_name . '.html';
		include_once $path;
	}

	public function load_model($model_name)
	{
		$path = KO_MODEL_PATH . $model_name . '.php';
		include_once $path;
	}

	public function load_controller($controller_name)
	{
		$path = KO_CONTROLLER_PATH . $controller_name . '.php';
		require_once $path;
		$controller_name = strtolower($controller_name);
		$controller = new $controller_name();
	}
}

/** ------------------------------------------------------
 *  Abstract Model
 * -------------------------------------------------------
 * Every model created by the user should extend this class. This will
 * allow a database connection to be opened automatically and provide
 * easy database querying. 
 *
 * The model class handles all database interfacing and is designed to
 * run queries and extract the data quickly and with ease.
 */
abstract class ko_model 
{
	private $db;

	public function __construct()
	{
		$db = new MySQL($ko_username, $ko_password, $ko_hostname);
		$db->set_database($ko_database);
	}
}

?>
Return current item: ko