2016-03-28 61 views
0

你好,我想做一个简单的配置文件为我的应用程序 我的项目文件夹: 文件夹“应用” -Config.php 根目录内内: -index.php -config.php错误与此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'); 

?> 

的事情是我得到这2个错误:

Notice: Undefined variable: data in C:\xampp\htdocs\probar\app\Config.php on line 11

Fatal error: Cannot access empty property in C:\xampp\htdocs\probar\app\Config.php on line 11

请帮帮我有什么不对这个???

回答

1

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

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

否则那些将是variable variables

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

感谢VolkerK它的工作! –

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; 
    } 
} 
+0

感谢您的评论,它工作@nerohc –