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

如何访问此PHP数组的内容?,php,class,arrays,Php,Class,Arrays,下面是PHP中的文件,我试图在类中包含一个包含数组的文件,然后访问该数组。如果我在上面运行print_r(),我会得到结果,但是如果我尝试单独访问数组项,我什么也得不到,有人能帮我吗 <?php // config.class.php /* example usages $config = Config::getInstance(PATH_TO_CONFIG_FILE, FILE_TYPE); echo $config->url; echo $config->test; ech

下面是PHP中的文件,我试图在类中包含一个包含数组的文件,然后访问该数组。如果我在上面运行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;
    }

}
?>

这是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
    )
);
?>

这就是我要做的

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

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

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

它返回一个数组,因此请使用

 echo $config['ip'];

它返回一个数组,因此请使用

 echo $config['ip'];

->用于类成员。您没有类,您有一个数组,所以请使用括号($config[xxxx]而不是$config->xxxx)。

->>用于类成员。你没有类,你有数组,所以用括号代替($config[xxxx]而不是$config->xxxx)。

你的代码对我来说很好。
检查您的PHP版本,v5.2.0之后支持此__; get

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

您的代码对我来说运行良好。
检查您的PHP版本,v5.2.0之后支持此__; get

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

我明白了,有没有办法通过_get()将我的数组转换成$config->ip?实际上我刚刚尝试了这个,我得到了这个错误致命错误:无法使用数组类型为config的对象我明白了,有没有办法通过_get()将我的数组转换成$config->ip?事实上,我刚刚尝试了这个,我得到了这个错误致命错误:无法使用类型为Config的对象作为arrayI刚刚尝试了,但得到。。。致命错误:无法使用配置类型的对象,因为arrayI刚刚尝试过,但是。。。致命错误:无法使用Config类型的对象,因为我只是在使用PHP5.2.5的IDE phpdesigner中运行它,如果我尝试运行echo$Config->ip,则不会出现错误;但它只是显示一个空白屏幕。然后,我在运行PHP5.2.9的localhost浏览器中尝试了它,效果很好。我只是在使用PHP5.2.5的IDE phpdesigner中运行了它。如果我尝试运行echo$config->ip,我不会出错;但它只是显示一个空白屏幕。然后我在运行PHP5.2.9的localhost浏览器中试用了它,效果很好