Hallo Ich versuche, eine einfache Konfigurationsdatei für meine app mein Projektordner zu machen ist: innerhalb Ordner ‚App‘ -Config.php innerhalb Stammverzeichnis: -index.php -config.phpFehler mit dieser PHP-Konfigurationsdatei
dies ist, wie config.php aussieht:
<?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;
}
}
?>
:
<?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 ist
und schließlich index.php:
<?php
use Project\Helpers\Config;
require 'app/Config.php';
$config = new Config;
$config->load('config.php');
echo $config->get('db.hosts.local');
?>
das Ding Ich erhalte dieses Bild 2 Fehler, wenn ich die Seite ausführen:
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
bitte helfen Sie mir, was ist falsch mit diesem ???
Dank VolkerK es funktionierte! –