I have a file localSettings.php which I am calling by using require() on each page. This is going to hold info like, the name of the application, database authentication details like username, password etc.
What’s the best way to incude this? At the moment, if I am using variables declared in it, I have to put the require() in that function, and if I have 5 seperate functions using the database authentication variables, I would have to include it in all those functions. Now I’m no PHP expert but I’m thinking that’s not a very efficient way of doing things?
See save() for an example. I am using the adodb database class which is what the $conn = NewADOConnection(‘mysql’); is for.
[code=php]<?php
if(!function_exists(‘__autoload’)) {
function __autoload($className){
require_once $className . ‘.php’;
}
}
class Incident {
public $inUser;
public $inItem;
public $inCat;
public $inDescription;
public $inStatus;
private $data = array();
public function __construct() {
}
public function __set($name, $value){
$this->data[$name] = $value;
}
public function __get($name){
return $this->data[$name];
}
function save($user,$item,$category,$description) {
require( ‘includes/localSettings.php’ );
require_once( ‘includes/adodb5/adodb.inc.php’ );
$conn = NewADOConnection(‘mysql’);
$conn->Connect($dbhost, $dbuser, $dbpass, $dbname);
$objUser = new User;
$objUser->load($user,’u_name’);
$this->inUser = $objUser->uId;
$objItem = new Item;
$objItem->load($item,’it_name’);
$this->inItem = $objItem->itId;
$objCat = new Category;
$objCat->load($category,’c_name’);
$this->inCat = $objCat->catId;
$sql = “INSERT INTO hd_incident (i_user,i_item,i_category,i_description,i_status,i_time_logged) VALUES (‘$this->inUser’,’$this->inItem’,’$this->inCat’,’$this->inDescription’,0, ” . time() .”)”;
if ($conn->Execute($sql) === false) {
print ‘<p class=”error”>error inserting: ‘ . $conn->ErrorMsg();
echo ‘<br />’ . $sql . ‘</p>’;
}
}
function displayForm() {
echo ‘
<form action=”Incident.php” method=”post”>
<label for=”username”>Username</label>
<input type=”text” name=”username” /><br />
<label for=”item”>Item</label>
<input type=”text” name=”item” /><br />
<label for=”category”>Category</label>
<input type=”text” name=”category” /><br />
<label for=”description”>Description</label>
<textarea name=”description” rows=”20″ cols=”60″></textarea><br />
<input name=”submit” value=”Submit” type=”submit” /><input name=”reset” value=”Reset” type=”reset” />
</form>
‘;
}
}
if($_POST){
$incident = new Incident;
$incident->inUser = $_POST[‘username’];
$incident->inItem = $_POST[‘item’];
$incident->inCat = $_POST[‘category’];
$incident->inDescription = $_POST[‘description’];
$incident->save($incident->inUser, $incident->inItem, $incident->inCat, $incident->inDescription);
}
?>