/    Sign up×
Community /Pin to ProfileBookmark

using an include with all config settings in classes

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);
}
?>
[/code]

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @Dopple spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...