简体   繁体   English

如何访问此PHP数组的内容?

[英]How can I access the contents of this PHP array?

Below are the files in PHP that I am trying to include a file that holds an array into my class and then access the array. 以下是PHP中要尝试包含的文件,该文件将数组保存到类中,然后访问该数组。 if I run print_r() on it I get results but if I try to access the array item individually I get nothing, can anyone help me please? 如果我在上面运行print_r(),我会得到结果,但是如果我尝试单独访问数组项,我什么也没有,有人可以帮我吗?

<?php
// config.class.php
/*
example usages
$config = Config::getInstance(PATH_TO_CONFIG_FILE, FILE_TYPE);
echo $config->url;
echo $config->test;
echo $config->ip;
*/

class Config
{
    private static $instance = null;
    private $options = array();

    /**
     * Retrieves php array file, json file, or ini file and builds array
     * @param $filepath Full path to where the file is located
     * @param $type is the type of file.  can be "ARRAY" "JSON" "INI"
     */ 
    private function __construct($filepath, $type = 'ARRAY')
    {
        switch($type) {
            case 'ARRAY':
                $this->options = include $filepath;
                break;

            case 'INI':
                $this->options = parse_ini_file($filepath, true);
                break;

            case 'JSON':
                $this->options = json_decode(file_get_contents($filepath), true);
                break;    
        }
    }

    private function __clone(){}

    public function getInstance($filepath, $type = 'ARRAY')
    {
        if (null === self::$instance) {
            self::$instance = new self($filepath, $type = 'ARRAY');
        }
        return self::$instance;
    }

    /**
     * Retrieve value with constants being a higher priority
     * @param $key Array Key to get
     */
    public function __get($key)
    {
        if (isset($this->options[$key])) {
            return $this->options[$key];
        }
    }

    /**
     * Set a new or update a key / value pair
     * @param $key Key to set
     * @param $value Value to set
     */
    public function __set($key, $value)
    {
        $this->options[$key] = $value;
    }

}
?>

And here is the config_array.ini.php file... 这是config_array.ini.php文件...

<?php
/**
 * @Filename config_array.ini.php
 * @description Array to return to our config class
 */
return array(
    'ip' => $_SERVER['REMOTE_ADDR'],
    'url' => 'http://www.foo.com',
    'db'  => array(
        'host' => 'foo.com',
        'port' =>  3306
    ),
    'caching' => array(
        'enabled' => false
    )
);
?>

Here is what I am trying to do... 这是我正在尝试做的...

   <?PHP
    $config = Config::getInstance('config_array.inc.php', 'ARRAY');

    // this does not show anything
    echo $config->ip;

    // this works
    print_r($config);
    ?>

The code you have is working fine for me. 您拥有的代码对我来说很好用。
Check your PHP version, this __get is supported after v5.2.0 检查您的PHP版本,此__get在v5.2.0之后受支持

echo $config->ip; // displays fine 127.0.0.1

它返回一个数组,所以使用...

 echo $config['ip'];

-> is used for class members. ->用于班级成员。 You don't have a class, you have an array, so use brackets instead ($config[xxxx] instead of $config->xxxx). 您没有类,但有一个数组,因此请使用方括号($ config [xxxx]代替$ config-> xxxx)。

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

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