2012-05-21 53 views
0

我有這個類我做這個工作,如果我保持類代碼中的常量,但我想從外部文件訪問它們,用戶可以評論或取消註釋C常數值。如何從PHP中的外部所需文件訪問常量?

它的偉大工程,這樣一來,但我不希望用戶四處翻找代碼:

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語句它不能找到,如果PWDALGO定義。

或這樣

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

我不知道這是可能的,因爲我不斷收到一個錯誤,我不認爲你可以包括或要求類範圍內的文件。

如果我得到這個工作,我想要一個安裝腳本來檢查服務器,看看有哪些加密類型可用,併爲用戶列出一個供用戶選擇的首選加密方法,然後設置它會自動爲他們。並且能夠稍後從管理控制面板更改加密方法。

+0

你在這約翰上編輯了什麼? –

+1

查看編輯歷史記錄,您可以看到所有更改。 – hakre

+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

+0

如果我誤解了你的問題,請告訴我。 –

+0

我嘗試了你在我的systemConfiguration.php中所說的:PWDALGO只會用於passwordStringHandler類,其他地方都會使用它,但我不希望最終用戶通過類文件進行翻頁來設置它。但是當我這樣做的時候,你說我得到了未定義的常量PWDALGO,當我這樣做self :: PWDALGO或$ this-> PWDALGO時,它會回到if/else語句的其他部分。這應該很簡單,爲什麼我這麼麻煩呢。 –

+0

我做了你所說的,它似乎仍然不工作,並感謝我今天早上閱讀的鏈接,它似乎沒有我想要做的。 –