私はこのクラスを持っています。
<?php
class Helper
{
private $config;
public function __construct(array $config)
{
$this->config = $config;
}
public function getVal($key)
{
return $this->config[$key];
}
}
構成は起動時に設定され、実行時に変更することはできません。そのため、特定の実行時に同じ引数を使用すると常に同じ結果が得られますが、プログラムの異なるインスタンスについて同じことを言うことはできません。
getVal(string)
純粋な関数と見なすことができますか?
同じ機能の別の非 OOP バージョンは次のようになります。
<?php
function getVal($key){
static $config;
if ($config === null) {
$config = include "config.php";
}
return $config[$key];
}