我正在尝试将密码敏感信息存储在单独的.php文件中。我能够将该文件包含在另一个php文件中(我们称之为php2.php)
问题是,我尝试在php1.php上使用的代码不想回显php2.php页面中指定的变量。在数组外的任何地方我都可以回显一个变量,它会显示它没有问题。我确定这只是一个语法错误,因为我开始学习PHP。
任何帮助表示赞赏!谢谢你的期待。
编辑:更新!
以下是我尝试使用的完整代码页,现在在第64行收到错误。
<?php
require_once 'login.php';
// dota2 api key (you can get_info it here - http://steamcommunity.com/dev/apikey)
$APIkey;
//The language to retrieve results in (see http://en.wikipedia.org/wiki/ISO_639-1 for the language codes (first two characters) and http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes for the country codes (last two characters))
define ('LANGUAGE', 'en_us');
error_reporting(0);
set_time_limit(0);
/**
* Basic class with system's configuration data
*/
class LoginInfo {
/**
* Configuration data
* @access private
* @static
* @var array
*/
private static $_data = array(
'APIkey' => '',
'db_user' => '',
'db_pass' => '',
'db_host' => '',
'db_name' => '',
'db_table_prefix' => ''
);
public static function SetVar ($APIkey, $dbuser, $dbpass, $dbhost, $dbname, $Prefix = null){
self:: $_data['APIkey'] = $APIkey;
self:: $_data['db_user'] = $dbuser;
self:: $_data['db_pass'] = $dbpass;
self:: $_data['db_host'] = $dbhost;
self:: $_data['db_name'] = $dbname;
if ($Prefix === null){
self::$_data['db_table_prefix'] = '';
}else{
self::$_data['db_table_prefix'] = $Prefix;
}
}
public static function GetInfo(){
return self::$_data;
}
/**
* Private construct to avoid object initializing
* @access private
*/
private function __construct() {}
public static function init() {
self::$_data['base_path'] = dirname(__FILE__).DIRECTORY_SEPARATOR.'includes';
$db = db::obtain(echo LoginInfo::GetInfo()['db_user'], echo LoginInfo::GetInfo()['db_pass'], echo LoginInfo::GetInfo()['db_host'], echo LoginInfo::GetInfo()['db_name'], echo LoginInfo::GetInfo()['db_user']);
if (!$db->connect_pdo()) {
die();
};
}
/**
* Get configuration parameter by key
* @param string $key data-array key
* @return null
*/
public static function get($key) {
if(isset(self::$_data[$key])) {
return self::$_data[$key];
}
return null;
}
}
config::init();
function __autoload($class) {
scan(config::get('base_path'), $class);
}
function scan($path = '.', $class) {
$ignore = array('.', '..');
$dh = opendir($path);
while(false !== ($file = readdir($dh))){
if(!in_array($file, $ignore)) {
if(is_dir($path.DIRECTORY_SEPARATOR.$file)) {
scan($path.DIRECTORY_SEPARATOR.$file, $class);
}
else {
if ($file === 'class.'.$class.'.php') {
require_once($path.DIRECTORY_SEPARATOR.$file);
return;
}
}
}
}
closedir($dh);
}
?>
答案 0 :(得分:1)
您的方法存在问题:对象的成员属性不接受表达式作为默认值。
我的意思是:
<?php
$foo = 'foo';
$bar = 'bar';
class Clazz {
public $foo = $bar;
static private $_data = array(
'foo' => $foo,
'bar' => $bar
);
}
尝试使用此代码here并亲眼看看。所有声明都是unexpected T_VARIABLE
的语法错误。
您的案例需要一种不同的方法,例如初始化方法或在其构造函数中设置$_data
的单例。
示例:
<?php
$foo = 'foo';
$bar = 'bar';
class Clazz {
public $foo = '';
static private $_data = array(
'foo' => '',
'bar' => ''
);
static private $_ininiated = false;
static public function init( ) {
if( !self::$_initiated ) {
self::$_data = array(
'foo' => $foo,
'bar' => $bar
);
self::$_initiated = true;
}
}
}
Singleton示例:
<?php
$foo = 'foo';
$bar = 'bar';
class Clazz {
static private $instance = null;
private $_data = array();
private function __construct( ) {
$this->_data = array(
'foo' => $foo,
'bar' => $bar
);
}
public function getData( $name ) {
return isset($this->data[$name]) ? $this->data[$name] : null;
}
static public function getInstance( ) {
if( self::$instance == null ) {
self::$instance = new self;
}
return self::$instance;
}
}
echo Clazz::getInstance()->getData('foo');
答案 1 :(得分:0)
假设你在课堂上有这个(因此允许private static
class Random {
private static $_Data = array(
'db_user' => '',
'db_pass' => '',
'db_host' => '',
'db_name' => '',
'db_table_prefix' => ''
);
public static function SetVar ($Host,$User,$Pass,$DB,$Prefix = null){
self::$_Data['db_user'] = $User;
self::$_Data['db_pass'] = $Pass;
self::$_Data['db_host'] = $Host;
self::$_Data['db_name'] = $DB;
if ($Prefix === null){
self::$_Data['db_table_prefix'] = '';
}else{
self::$_Data['db_table_prefix'] = $Prefix;
}
}
public static function GetInfo(){
return self::$_Data;
}
}
然后拨打以下电话:
Random::SetVar("Test","Test","Test","Test");
print_r(Random::GetInfo());
甚至可以访问:
echo Random::GetInfo()['db_user'];
但是如果您使用的旧版PHP不支持上述调用类型:
$Var = Random::GetInfo();
echo $Var['db_user'];