language/php

[php] 설정파일 패턴들

alignG 2009. 10. 20. 04:16
일반파일부터 xml까지 다양하게 파일에서 읽어와 사용할수 있는 설정파일들에 대한 설명들이 있다

url : PHP configuration patterns
파일을 이용한 설정파일 읽어들이기 

config.txt
# My application's configuration file
Title=My App
TemplateDirectory=tempdir


<?php
class Configuration
{
private $configFile = 'config.txt';

private $items = array();

function __construct() { $this->parse(); }

function __get($id) { return $this->items[ $id ]; }

function parse()
{
$fh = fopen( $this->configFile, 'r' );
while( $l = fgets( $fh ) )
{
if ( preg_match( '/^#/', $l ) == false )
{
preg_match( '/^(.*?)=(.*?)$/', $l, $found );
$this->items[ $found[1] ] = $found[2];
}
}
fclose( $fh );
}
}

$c = new Configuration();

echo( $c->TemplateDirectory."\n" );
?>


반응형