如果我将常量保留在类代码中,我有这个类我可以使用,但我想从外部文件访问它们,用户可以注释或取消注释c常量值。
这种方式效果很好,但我不希望用户在代码中翻找:
class passwordStringHandler
{
# const PWDALGO = 'md5';
# const PWDALGO = 'sha1';
# const PWDALGO = 'sha256';
# const PWDALGO = 'sha512';
const PWDALGO = 'whirlpool';
/* THIS METHOD WILL CREATE THE SALTED USER PASSWORD HASH DEPENDING ON WHATS BEEN
DEFINED */
function createUsersPassword()
{
$userspassword = 'Te$t1234';
$saltedpassword='';
if ((defined('self::PWDALGO')) && (self::PWDALGO === 'md5'))
{
$saltedpassword = md5($userspassword . $this->pwdsalt);
echo("The salted md5 generated hash is: " . $saltedpassword . "<br>");
return $saltedpassword;
}elseif ((defined('self::PWDALGO')) && (self::PWDALGO === 'sha1')){
$saltedpassword = sha1($userspassword . $this->pwdsalt);
echo("The salted sha1 generated hash is: " . $saltedpassword . "<br>");
return $saltedpassword;
}elseif ((defined('self::PWDALGO')) && (self::PWDALGO === 'sha256')){
$saltedpassword = hash('sha256', $userspassword . $this->pwdsalt);
echo("The salted sha256 generated hash is: " . $saltedpassword . "<br>");
return $saltedpassword;
}elseif ((defined('self::PWDALGO')) && (self::PWDALGO === 'sha512')){
$saltedpassword = hash('sha512', $userspassword . $this->pwdsalt);
echo("The salted sha512 generated hash is: " . $saltedpassword . "<br>");
return $saltedpassword;
}elseif ((defined('self::PWDALGO')) && (self::PWDALGO === 'whirlpool')){
$saltedpassword = hash('whirlpool', $userspassword . $this->pwdsalt);
echo("The salted whirlpool generated hash is: " . $saltedpassword . "<br>");
return $saltedpassword;
}
else
echo("No password algro is defined! Edit the [<strong>PWDALGO</strong>] options in the <strong>systemConfiguration.php</strong><br>");
return false;
}
这可以正常工作,因为它被硬编码到类文件中:
我想用它来工作:
require ("../configs/systemConfiguration.php");
class passwordStringHandler
{
如果定义了PWDALGO,我一直在if / else语句中找到else。
或者这样
class passwordStringHandler
{
require ("../configs/systemConfiguration.php");
我不知道这是否可行,因为我一直收到错误,我认为你不能在类范围内包含或要求文件。
将来如果我开始工作,我想要一个安装脚本检查服务器以查看可用的加密类型,并列出供用户选择的首选加密方法,然后设置它自动为他们。并且稍后可以从管理控制面板更改加密方法。
答案 0 :(得分:1)
听起来您希望这些常量跨越对象(类),而不仅限于passwordStringHandler
类。
如果 ,我建议您使用define()
代替const
。
像这样:
<强> systemconfiguration.php 强>
define('PWDALGO', 'whirlpool');
<强> passwordStringHandler.php 强>
require ("../configs/systemConfiguration.php");
class passwordStringHandler
{
if ((defined('PWDALGO')) && (PWDALGO === 'md5'))
更多信息: define() vs const