Pogledajte određenu poruku
Staro 10. 03. 2009.   #1
mb_sa
profesionalac
Qualified
 
Datum učlanjenja: 19.05.2007
Poruke: 123
Hvala: 13
3 "Hvala" u 3 poruka
mb_sa is on a distinguished road
Default Pozivanje metoda jedne klase u drugim klasama

Zdravo svima.

Zanima kako je najbolje pozivati funkcije (metode) neke DB klase u drugim klasama (recimo News class) a da to ispoštuje principe OOP, a da ima nekog smisla tako raditi.

Jasno mi je da ovdje nema smisla ici sa nasljijedivanjem.

Jesam li upravu ako kazem da je najlogicnije kreirati objekat Database klase unutar zeljene funkcije druge klase (News klase) i onda pozivati željene funckije Database klase?!

Osakacena verzija DB kalse je ispod, kao i klasa News sa insertNews metodom.

Bilo kakva pomoc, link na slicnu tematiku?


Kôd:
<?php

//author: http://www.milesj.me/

define('DB_HOST', 'localhost'); 
define('DB_NAME', 'sportsport');
define('DB_USER', 'root');  
define('DB_PASS', 'root');

class Database { 

	/**
	 * Contains the database instance
	 * @property instance
	 */
	private static $instance;
	
	/**
	 * Holds the database connection link
	 * @property mixed
	 */
	private $connection = NULL;
	
	/**
	 * Contains database connection information
	 * @property array
	 */
	private $db = array(
		'server'	=> DB_HOST,
		'database'	=> DB_NAME,	
		'username'	=> DB_USER,			
		'password'	=> DB_PASS
	);
	
	/**
	 * If you want data returned as an object instead of an array
	 * @property boolean
	 */
	private $asObject = false;
	
	/**
	 * Connects to the database on class initialize; use getInstance()
	 * @return void
	 */
	private function __construct() {
		$this->connect();
	}
	
	/**
	 * Connects and returns a single instance of the database connection handle
	 * @return instance
	 */
	public static function getInstance() {
		if (!isset(self::$instance)){
			self::$instance = new Database();
		}
		
		return self::$instance;
	} 

	/**
	 * Attempts to connect to the MySQL database
	 * @return boolean
	 */
	private function connect() {
		$this->queries = array();
		$this->executed = 0;
		$this->connection = mysql_connect($this->db['server'], $this->db['username'], $this->db['password']);
	
		if ($this->connection) {
			if (!mysql_select_db($this->db['database'], $this->connection)) {
				trigger_error('Database::connect(): '. mysql_error() .'. ('. mysql_errno() .')', E_USER_ERROR); 
			}
		}
		
		unset($this->db['password']);
		return $this->connection;
	}

	/**
	 * Executes the sql statement after being prepared and binded
	 * @param string $sql
	 * @return mixed
	 */
	public function execute($sql) {
		$result = mysql_query($sql, $this->connection);

		if ($result === false) {
			$failure = mysql_error() .'. ('. mysql_errno() .')';
			trigger_error('Database::execute(): '. $failure, E_USER_ERROR); 
		} 
		
		return $result;
	} 
	
	/**
	 * Fetches all rows from the query
	 * @param result $query
	 * @param bool $asObject
	 * @return array
	 */
	public function fetchAll($query) {
		if ($this->asObject === true) {
			$result = mysql_fetch_object($query);
		} else {
			$result = mysql_fetch_assoc($query);
		}
		
		return $result;
	}
	
}


Kôd:
class News {

public function insertNews() {

// pozvati funkciju execute($sql) DataBase klase
// najbolji i najispravniji nacin?

}


}
mb_sa je offline   Odgovorite uz citat