Php 试图从include回显私有静态数组内部的变量
我试图将密码敏感信息存储在一个单独的.php文件中。我可以将该文件包含在另一个php文件中(我们称之为php2.php) 问题是,我试图在php1.php上使用的代码不希望回显php2.php页面中指定的变量。在数组之外的任何地方,我都可以回显一个变量,它将毫无问题地显示它。我确信这只是一个语法错误,因为我正开始学习PHP 感谢您的帮助!谢谢你的关注 编辑:更新 这是我试图使用的代码的完整页面,现在第64行出现了一个错误Php 试图从include回显私有静态数组内部的变量,php,arrays,Php,Arrays,我试图将密码敏感信息存储在一个单独的.php文件中。我可以将该文件包含在另一个php文件中(我们称之为php2.php) 问题是,我试图在php1.php上使用的代码不希望回显php2.php页面中指定的变量。在数组之外的任何地方,我都可以回显一个变量,它将毫无问题地显示它。我确信这只是一个语法错误,因为我正开始学习PHP 感谢您的帮助!谢谢你的关注 编辑:更新 这是我试图使用的代码的完整页面,现在第64行出现了一个错误 <?php require_once 'l
<?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);
}
?>
您的方法有一个问题:对象的成员属性不接受表达式作为默认值 我的意思是:
<?php
$foo = 'foo';
$bar = 'bar';
class Clazz {
public $foo = $bar;
static private $_data = array(
'foo' => $foo,
'bar' => $bar
);
}
单例示例:
<?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');
假设你在一个类中有这个(因此允许私有静态
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'];
哦,我现在明白你的问题了。我确定问题出在哪里,我尝试过只使用echo$变量,但在“”中它不喜欢它。谢谢,现在在另一行(64)中出现错误。它不喜欢使用(echo…)。这是页面的完整代码…它不允许我将代码发布到评论中,请查看我粘贴的原始问题。谢谢!@Tandar如果您仍然存在此问题,我很抱歉直到现在才回复。请让我知道您是否仍然卡住,我为您服务嗨!是的,没关系,我知道每个人都有自己的事情要做:)我一直在修补它,但没有取得太大的进展,任何更多的帮助将不胜感激。我已经考虑过重新开始,甚至使用一种不同的方法。@Tandar你坚持的是什么?尽可能多地更新您的问题possible@Daryll它不喜欢我尝试对设置为拉入敏感数据的类使用echo。echo LoginInfo::GetInfo()['dbuser']更准确地说
$Var = Random::GetInfo();
echo $Var['db_user'];