/    Sign up×
Community /Pin to ProfileBookmark

Assigning variables to CONST

I am having a problem assigning variables to constants in a class. I am using Authorize.net AIM class and want to assign the login and transaction key that is stored in a database. However, it doesn’t allow it and in the code below, I have to use the actual string to make it work.

Can someone help me out on how to make this work with the const variables?

Thanks in advance.

[CODE]
<?php
include(“../include/connectionString.php”);
$query = mysql_query(“SELECT * FROM admin”);
while ($get = mysql_fetch_array($query)) {
$login = $get[‘authnetLogin’];
$transKey = $get[‘authnetTranskey’];
}

class AuthnetAIMException extends Exception {}

class AuthnetAIM
{

const LOGIN = ‘123456’;
const TRANSKEY = ‘123456’;

//trying to assign variables here from query above….
// const LOGIN = $login;
// const TRANSKEY = $transKey;

const TEST = false;
const GODADDY = false;

private $params = array();
private $results = array();
private $approved = false;
private $declined = false;

..rest of code

[/CODE]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiApr 30.2009 — The variables are not set in the class' scope so it wont work. However why would you want to assign a variable to a constant - that's a total oxymoron. A constant should not hold variable data, that's what a variable is for.
Copy linkTweet thisAlerts:
@UltimaterMay 03.2009 — Eval can accomplish this. I'm also in the habit of placing constants in an interface.
[code=php]
<?php
$login = "3fda03b570a00aaa";
$transKey = "cba720c1a09e";
eval(<<<EOD
interface login_transkey
{
const LOGIN='$login';
const TRANSKEY='$transKey';
}
EOD
);


class hey implements login_transkey
{
public function display()
{
print_r(array(self::LOGIN,self::TRANSKEY));
}
}

hey::display();
?>
[/code]
Copy linkTweet thisAlerts:
@NogDogMay 03.2009 — In the spirit of, "If eval() is the answer, you're probably asking the wrong question," I would opt for rethinking the class design and probably go with class variables instead of constants if, in fact, you need those attributes to be modifiable at run-time.
×

Success!

Help @our5 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...