/    Sign up×
Community /Pin to ProfileBookmark

Are there any security issues with constants?

In my config file I currently have an array containing all the values/settings required for the site/system.

This becomes a pain with OO as i ma always having to pass these into objects/functions.

Are there any security issues with using constants to store DB connection values etc…?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 11.2007 — None that I know of. If anything, they should be more secure than variables since once they are set, they can not be changed.

But perhaps you should consider a "config" class with each of those values as class constants:
[code=php]<?php

class Config
{
const constant_name = 'some value';
}

class Example
{
function getConstant()
{
return Config::constant_name;
}
}

$test = new Example();
echo $test->getConstant(); // outputs "some value"
?>
[/code]
Copy linkTweet thisAlerts:
@knowjauthorNov 11.2007 — then have it as an extension of my database class?
Copy linkTweet thisAlerts:
@NogDogNov 11.2007 — I was visualizing the configuration constants just being in a separate class, which could then be require_once()'ed with any file that needs access to them; but there is no particular reason they could not exist in any desired class. Also, if the value needs to be modifiable, you specify it as [b]static[/b] instead of [b]const[/b]. In either case, they must be referenced as [b]ClassName::constant_name[/b] rather than via [b]objectName->constant_name[/b].

On a related (?) note, you might want to consider defining your database class as a "singleton" class (see [url=http://www.php.net/manual/en/language.oop5.patterns.php]Patterns[/url]). Then once that class is instantiated as an object in any object, all further attempts to instantiate it actually get a copy of the same database object.
×

Success!

Help @knowj 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.23,
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,
)...