xmlからクレデンシャルを読み取り、phpを使用してプライベート変数に初期化しようとしています。
<?xml version="1.0" encoding="ISO-8859-1"?>
<config>
<host>localhost</host>
<username>Jani</username>
<password>1234r</password>
</config>
次のクラスとコンストラクターを使用して変数を初期化します。
<?php
class Admin {
public $host = "";
private $username;
private $password;
private $table;
private $crendentials = array();
public function __construct() {
$xml = simplexml_load_file("config.xml");
foreach ($this->crendentials as $key => $value) {
if ($key == $this->host) {
$this->host = $value;
} elseif ($key == $this->username) {
$this->username = $value;
} elseif ($key == $this->password) {
$this->password = $value;
}
}
foreach ($xml->children() as $child) {
$this->crendentials[$child->getName()] = $child;
}
}
public function getHost() {
print_r($this->crendentials);
echo $this->host;
echo $this->username;
echo $this->password;
}
}
$obj = new Admin;
$obj->getHost();
?>
変数を初期化するのではありません。それ以外に、クレデンシャルをロードする良い方法はありますか?私はcmsを作成しています。iniファイルを使用するなどの調査を行いました。しかし、xmlが最も簡単だと思います。