这个PHP配置文件出错

时间:2016-03-28 03:21:18

标签: php

您好我正在尝试为我的应用制作一个简单的配置文件 我的项目文件夹是: 内部文件夹'app'        -Config.php 在根目录中: -index.php -config.php

这就是config.php的样子:

<?php  
return [
    'db' => [
        'hosts' => [
            'local' => 'localhost',
            'externo' => '1.1.1.1',
        ],
        'name' => 'db-stats',
        'user' => 'root',
        'password' => 'root'
    ],
    'mail' => [
        'host' => 'smtp.gmail.com'
    ]
];

?>

Config.php是:

<?php 
namespace Project\Helpers;
class Config
{
    protected $data;
    protected $default = null;
    public function load($file){
        $this->$data = require $file;
    }
    public function get($key, $default = null){
        $this->$default = $default;
        $segments = explode('.', $key);
        $data = $this->$data;

        foreach ($segments as $segment) {
            if(isset($data[$segment])){
                $data = $data[$segment];
            }else{
                $data = $this->$default;
                break;
            }
        }
        return $data;
    }
    public function exists($key){
        return $this->get($key) !== $this->$default;
    }
}
?>

最后是index.php:

<?php 

use Project\Helpers\Config;
require 'app/Config.php';

$config = new Config;
$config->load('config.php');

echo $config->get('db.hosts.local');

?>

问题是我在运行页面时遇到了这两个错误:

  

注意:未定义的变量:数据输入   第11行的C:\ xampp \ htdocs \ probar \ app \ Config.php

     

致命错误:无法访问空属性   第11行的C:\ xampp \ htdocs \ probar \ app \ Config.php

请帮我解决这个问题???

2 个答案:

答案 0 :(得分:1)

$this->default = $default; $ this-&gt; $ data = require $ file;

<?php namespace Project\Helpers; class Config { protected $data; protected $default = null; public function load($file){ $this->data = require $file; } public function get($key, $default = null){ $this->default = $default; $segments = explode('.', $key); $data = $this->data; foreach ($segments as $segment) { if(isset($data[$segment])){ $data = $data[$segment]; }else{ $data = $this->default; break; } } return $data; } public function exists($key){ return $this->get($key) !== $this->default; } } 代替 $ this-&gt; $ default = $ default;

否则这些将是variable variables

if (ChooseColor(&cc) == TRUE) {
    HBRUSH hbrush = CreateSolidBrush(cc.rgbResult);
    HBRUSH hOldBrush = (HBRUSH)SetClassLongPtr(hWnd, GCLP_HBRBACKGROUND, (LONG_PTR)hbrush);

    DeleteObject(hOldBrush);
    InvalidateRect(hWnd, NULL, 1);
}

答案 1 :(得分:1)

类构造函数中存在synthax错误。在PHP中,当您使用->运算符访问成员属性时,您不必使用$修饰符。

正确的代码如下所示:

<?php
namespace Project\Helpers;
class Config
{
    protected $data;
    protected $default = null;
    public function load($file){
        $this->data = require $file;
    }
    public function get($key, $default = null){
        $this->default = $default;
        $segments = explode('.', $key);
        $data = $this->data;

        foreach ($segments as $segment) {
            if(isset($data[$segment])){
                $data = $data[$segment];
            }else{
                $data = $this->default;
                break;
            }
        }
        return $data;
    }
    public function exists($key){
        return $this->get($key) !== $this->default;
    }
}