Sorry, my question is not clear. I will explane it better.
First of all, I have the DB-connection removed from the controller en views, so that solved. The code looks like below at the moment (Controller and View are simplified for this post).
Controller.php:
PHP Code:
<?php
/**
* The Controller of the apllication
*
* Starts the correct view and performs the action that are requested.
* @autor Rémy van Duijkeren <[email=mail2remy@chello.nl]mail2remy@chello.nl[/email]>
* @copyright 2003 RéMy Productions
* $version 0.1
* @package application
*/
Class Controller {
/**
* Containts a array of constants
* @access private
* @var array
*/
var $constants;
/**
* Contains the template object
* @access private
* @var object
*/
var $template;
/**
* Constructor of the application.
*
* Put all constants in a array, create the
* templateObject and a DbConnectionObject
* @access private
*/
function Controller() {
// get constants for the application and sets ini-settings
include_once ("settings.php");
$this->constants = array_merge($_POST, $_GET, $_FILES, $APPLICATION);
ini_set("display_errors",$this->constants['display_errors']);
ini_set("log_errors",$this->constants['log_errors']);
ini_set("error_log",$this->constants['error_log']);
ini_set("output_buffering",$this->constants['output_buffering']);
// create TemplateObject
include_once ($this->constants['pathUtility']."/Template.php");
$this->template =& new Template;
$this->template->setTemplateDir($this->constants['pathTemplate']);
}
/**
* Starts the apllication frame work
* @access public
*/
function run() {
if ($this->constants['online']) {
$this->template->addVar("title","Homepage");
$this->template->addVar("content", $this->excuteAction($this->constants['action']));
} else {
$this->template->addVar("title","under construction");
$this->template->addVar("content", $this->template->parse("000underConstruction.tpl.htm"));
}
$this->template->display("Layout.tpl.htm");
}
/**
* Execute the requested action
* @param string $class The class name
* @param string $method The method name
* @access private
* @return string all parsed HTML-code of the action or FALSE
*/
function excuteAction($action) {
// separate the action in class and method
if (isset($this->constants['action'])){
$class = strtok($this->constants['action'], '.');
$method = strtok('');
} else {
$class = "Homepage";
$method = "doDefault";
}
if (file_exists ($this->constants['pathApplication']."/".$this->constants['pathView']."/".$class.".php")) {
include_once($this->constants['pathView']."/".$class.".php");
$class =& new $class($this->constants, $this->db, $this->template);
$result = (method_exists($class, $method)) ? $class->$method() : $this->pageNotFound() ;
} else {
$result = $this->pageNotFound();
}
return (isset($result)) ? $result : false;
}
/**
* Shows a page-not-found page
* @access private
* $return string Contains all parsed HTML-code
*/
function pageNotFound() {
$this->template->addVar("title","404 Not Found");
return $this->template->parse("404notFound.tpl.htm");
}
}
?>
HomepageView.php:
PHP Code:
<?php
include_once("View.php");
/**
* The Hompage view
*
* This class takes care of the view of the homepage.
* @autor Rémy van Duijkeren <[email=mail2remy@chello.nl]mail2remy@chello.nl[/email]>
* @copyright 2003 RéMy Productions
* $version 0.1
* @package application
*/
Class HomepageView extends View {
/**
* Constructor of the view.
*
* Calls the constructer of the parent-class.
* @access private
*/
function HomepageView(&$constants, &$template) {
$this->View($constants, $template);
}
/**
* The default action
* @access public
*/
function doDefault() {
// example of use of businessObjects!
include_once($this->constants['pathBusinessObjects']."/Department.php");
include_once($this->constants['pathBusinessObjects']."/Employee.php");
if ( isset($_SERVER["LOGON_USER"]) ) {
$Employee =& new Employee(substr($_SERVER["LOGON_USER"],-3));
} else { Header("HTTP/1.0 401 Unauthorized");}
$Department =& new Department($Employee->getDepartmentId(), $Employee->getDbConnection());
$this->template->addVar("username", $Employee->getFirstName().$Employee->getLastName());
$this->template->addVar("department", $Department->getName());
$this->template->addVar("articletitel", $article->getTitel());
$this->template->addVar("articlebody", $article->getBody());
return $this->template->parse("userinfo.tpl.htm");
}
}
?>
BusinessLogic.php:
PHP Code:
<?php
/**
* Business Logic Class
*
* The base class of which all businessObject should extend.
* @author Rémy van Duijkeren
* @version 0.5
*/
Class BusinessLogic {
/**
* Contains the database object (the DbConnection)
* @access private
* @var object
*/
var $db;
/**
* Containts a array of constants
* @access private
* @var array
*/
var $constants;
/**
* Constructor of all Business Objects.
*
* Put all constants in a array and create a DatabaseObject.
* This should be called in each constructer class based on this class.
* @access protected
*/
function BusinessLogic() {
// get constants
include ("settings.php");
$this->constants = $APPLICATION;
// create DatabaseObject
include_once ($this->constants['pathLibary']."/eclipse/MSDatabase.php");
$this->db =& new MSDatabase($this->constants['dbName'], $this->constants['dbHost']) or die ($db->getErrorMessage());
$this->db->connect($this->constants['dbUser'], $this->constants['dbPassword']) or die ($db->getErrorMessage());
}
}
?>
Department.php:
PHP Code:
<?php
include_once ("BusinessLogic.php");
/**
* Department Class
*
* @author Rémy van Duijkeren
* @version 0.5
*/
Class Department extends BusinessLogic {
var $Id;
var $Name;
function Department($id) {
$this->BusinessLogic();
$result =& $this->db->query("SELECT * FROM department WHERE department_id = ".$id);
if ($row =& $result->getRow(0)) {
$this->Id = $row['department_id'];
$this->Name = $row['name'];
return true;
} else {
return false;
}
}
function getId() {
return $this->Id;
}
function getName() {
return $this->Name;
}
}
?>
Employee.php:
PHP Code:
<?php
include_once ("BusinessLogic.php");
/**
* Employee Class
*
* @author Rémy van Duijkeren
* @version 0.5
*/
Class Employee extends BusinessLogic {
var $Id;
var $DepartmentId;
var $FirstName;
var $LastName;
function Employee($id) {
$this->BusinessLogic();
$result =& $this->db->query("SELECT * FROM employee WHERE employee.employee_id = '".$id."'");
if ($row =& $result->getRow(0)) {
$this->Id = $row['employee_id'];
$this->DepartmentId = $row['department_id'];
$this->FirstName = $row['first_name'];
$this->LastName = $row['last_name'];
return true;
} else {
return false;
}
}
function getId() {
return $this->Id;
}
function getDepartmentId() {
return $this->DepartmentId;
}
function getFirstName() {
return $this->FirstName;
}
function getLastName() {
return $this->LastName;
}
}
?>
The database is now only used by the businessObjects, but with each creation of a bussenissObject(Employee and Department) a new connection is been made to the database (if I am not wrong). I read some of the posts on this forum that are telling that this is bad, I should pass the dbconnection as reference.
The problem is how to do this the 'right way'? The only thing I can think of is put a extra method, getDbConnection(), in the class 'BusinessLogic' and change the constructor. The constructor should create a DbConnection if not given as parameter.
The change in the HomepageView should be something like this:
PHP Code:
$Department =& new Department($Employee->getDepartmentId(), $Employee->getDbConnection());
Is this the 'right way'? Also if there are other thing I am not getting or doing wrong in my code, please criticise me, I am still new to OOP and want to learn the 'right way' to do OOP. images/smilies/smile.gif
-Rémy
(Why aren't enters display in PHPtag?)
Bookmarks