[英]How do I access constants from a external required file in PHP?
如果我将常量保留在类代码中,则可以使用该类,但我想从外部文件访问它们,以便用户可以注释或取消注释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
{
我一直在我的if / else语句中获取else,它找不到PWDALGO是否已定义。
或者这样
class passwordStringHandler
{
require ("../configs/systemConfiguration.php");
我不知道这是否可行,因为我不断收到错误消息,我认为您不能在类范围内包含或要求文件。
将来,如果我可以使用它,我希望安装脚本可以检查服务器,以查看可用的加密类型,并列出可供用户选择的首选加密方法,然后为它们自动设置加密方法的列表。 。 并且以后可以在管理控制台中更改加密方法。
听起来像您希望这些常量跨越对象(类),而不是仅限于passwordStringHandler
类。
如果是这种情况,我建议您使用define()
而不是const
。
像这样:
systemconfiguration.php
define('PWDALGO', 'whirlpool');
passwordStringHandler.php
require ("../configs/systemConfiguration.php");
class passwordStringHandler
{
if ((defined('PWDALGO')) && (PWDALGO === 'md5'))
更多信息在这里: define()vs const
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.