此PHP配置文件出错


Error with this PHP config file

你好,我正在为我的应用程序制作一个简单的配置文件我的项目文件夹是:在文件夹"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');
?>

问题是,当我运行页面时,我得到了这两个错误:

注意:未定义的变量:中的数据C: 第11行上的''axamp''htdocs''probar''app''Config.php

致命错误:无法访问中的空属性C: 第11行上的''axamp''htdocs''probar''app''Config.php

请帮帮我这个怎么了???

$this->data = require $file;不是$this->$data=require$file。

并且$this->default = $default;而不是$this->$default=$default

否则,这些将是可变变量。

<?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;
    }
}

类构造函数中存在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;
    }
}