尝试从包含回显私有静态数组内的变量


Trying to echo variable inside of private static array from include

>我正在尝试将密码敏感信息存储在单独的.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);
        }
?>

您的方法存在问题:对象的成员属性不接受表达式作为默认值

我的意思是:

<?php
$foo = 'foo';
$bar = 'bar';
class Clazz {
    public $foo = $bar;
    static private $_data = array(
        'foo' => $foo,
        'bar' => $bar
    );
}

在这里尝试此代码,自己看看。所有声明都是带有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;
        }
    }
}

单例示例:

<?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');

假设您在一个类中有这个(因此允许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'];