扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:Jack D. Herrington 来源:IBM 2007年10月21日
关键字:
# 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" ); ?> |
% php text1.php tempdir % |
<?php class Configuration { ... function __get($id) { return $this->items[ $id ]; } function __set($id,$v) { $this->items[ $id ] = $v; } function parse() { ... } } $c = new Configuration(); echo( $c->TemplateDirectory."\n" ); $c->TemplateDirectory = 'foobar'; echo( $c->TemplateDirectory."\n" ); ?> |
% php text2.php tempdir foobar % |
<?php class Configuration { ... function save() { $nf = ''; $fh = fopen( $this->configFile, 'r' ); while( $l = fgets( $fh ) ) { if ( preg_match( '/^#/', $l ) == false ) { preg_match( '/^(.*?)=(.*?)$/', $l, $found ); $nf .= $found[1]."=".$this->items[$found[1]]."\n"; } else { $nf .= $l; } } fclose( $fh ); copy( $this->configFile, $this->configFile.'.bak' ); $fh = fopen( $this->configFile, 'w' ); fwrite( $fh, $nf ); fclose( $fh ); } } $c = new Configuration(); echo( $c->TemplateDirectory."\n" ); $c->TemplateDirectory = 'foobar'; echo( $c->TemplateDirectory."\n" ); $c->save(); ?> |
% php text3.php tempdir foobar % cat config.txt # My application's configuration file Title=My App TemplateDirectory=foobar % |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。