简体   繁体   English

如何从PHP的外部必需文件访问常量?

[英]How do I access constants from a external required file in PHP?

I have this class i making that works if I keep the constants in the class code, but i wanted to access them from an external file that the user can comment or uncomment out c constant value. 如果我将常量保留在类代码中,则可以使用该类,但我想从外部文件访问它们,以便用户可以注释或取消注释c常量值。

it works great this way but I do not want the users rummaging around in the code: 这样可以很好地工作,但是我不希望用户在代码中四处乱逛:

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;

}

This works ok cause it is hard coded into the class file: 这行得通,因为它已被硬编码到类文件中:

I want it to work using this: 我希望它使用此功能:

require ("../configs/systemConfiguration.php");   
class passwordStringHandler
{

I keep getting the else in my if /else statement it cant find if PWDALGO is defined. 我一直在我的if / else语句中获取else,它找不到PWDALGO是否已定义。

or this way 或者这样

class passwordStringHandler
{
require ("../configs/systemConfiguration.php");

I do not know if this is possible since i keep getting an error, I do not think you can include or require files inside the class scope. 我不知道这是否可行,因为我不断收到错误消息,我认为您不能在类范围内包含或要求文件。

in the future if I get this to work I wanted a setup script to check the server to see what encryption types are available and to make a list of the for the user to choose there preferred method of encryption, and then set it automatically for them. 将来,如果我可以使用它,我希望安装脚本可以检查服务器,以查看可用的加密类型,并列出可供用户选择的首选加密方法,然后为它们自动设置加密方法的列表。 。 and be able to change the encryption method later from a admin control panel. 并且以后可以在管理控制台中更改加密方法。

Sounds like you want these constants to span across objects (classes), and not to be constrained to just the passwordStringHandler class. 听起来像您希望这些常量跨越对象(类),而不是仅限于passwordStringHandler类。

If that is the case, I'd suggest you go with define() instead of const . 如果这种情况,我建议您使用define()而不是const

Like this: 像这样:

systemconfiguration.php systemconfiguration.php

define('PWDALGO', 'whirlpool');

passwordStringHandler.php passwordStringHandler.php

require ("../configs/systemConfiguration.php");

class passwordStringHandler
{
    if ((defined('PWDALGO')) && (PWDALGO === 'md5'))

More info here: define() vs const 更多信息在这里: define()vs const

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM