PHP是否允许* .properties文件与Java一样?

有没有办法在PHP中使用* .properties文件,就像在Java中一样? 我想在属性或XML文件中存储一些应用程序级常量,并在我的代码中轻松调用它们。 非常感谢您的指导。 谢谢。

PHP可以使用parse_ini_file()本机加载和解析.ini文件。

您还可以使用define()在包含文件中设置常量。

如果您设置了XML,请查看PHP的XMLfunction。 最简单的解决方案可能是使用SimpleXML 。

您还可以使用包含数组的PHP文件来存储数据。 例:

config.php文件

  'localhost', 'title' => 'My app' ); 

然后在另一个文件中:

 $config = require 'config.php': echo $config['title']; 

parse_ini_file与Java环境中的*.properties文件无关。

我创建了这个函数,它与Java中的等价函数完全相同:

 function parse_properties($txtProperties) { $result = array(); $lines = split("\n", $txtProperties); $key = ""; $isWaitingOtherLine = false; foreach($lines as $i=>$line) { if(empty($line) || (!$isWaitingOtherLine && strpos($line,"#") === 0)) continue; if(!$isWaitingOtherLine) { $key = substr($line,0,strpos($line,'=')); $value = substr($line,strpos($line,'=') + 1, strlen($line)); } else { $value .= $line; } /* Check if ends with single '\' */ if(strrpos($value,"\\") === strlen($value)-strlen("\\")) { $value = substr($value, 0, strlen($value)-1)."\n"; $isWaitingOtherLine = true; } else { $isWaitingOtherLine = false; } $result[$key] = $value; unset($lines[$i]); } return $result; } 

此function首次发布在我的博客上 。

好吧,你可以完美地将一些配置放在属性文件中并自己进行解析。 但在PHP中,它并不是适当的格式。

我将定义一些常量并将它们放在一个单独的php配置文件(如config.php)中,并在需要的地方包含它。

其他选项是将配置实际放入xml文件并使用xml库读取它。 YAML ( php.net )也是简单可读配置的流行选项。

据我所知,没有PHP内置函数来读取和处理.properties文件。 如果您在PHP中编写的应用程序需要读取Java .properties文件,则需要通过读取文件并创建自己的.properties解析器来编写自己的解决方案。

否则,如此处其他成员所提到的,您可以将配置信息存储到.ini文件中。 PHP提供了加载和解析.ini文件的本机函数。

如果您愿意,可以使用PHP XML解析器( PHP手册函数参考XML操作 )将信息存储到XML文件中。

就个人而言,我更喜欢将配置/应用程序常量值存储到数组中。 我创建了一个处理配置文件的Configuration类。

这里有一个例子:

配置文件示例(例如:config.php)

  

class级配置

  define('CFG_PATH',dirname(__FILE__) . '/cfg/'); require(CFG_PATH . $filename); $this->setConfiguration($config); } public function getConfiguration($configuration) { return $this->configuration[$configuration]; } public function setConfiguration($configuration) { $this->configuration = $configuration; } } ?> 

典型用例

 configuration = new Configuration(CFG_FILE); $this->set_Timezone($this->configuration->getConfiguration('date')); } private function set_Timezone($configuration) { date_default_timezone_set($configuration['timezone']); } ?> 

其他例子(Controller和DAO)

调节器

 getConfiguration('database')); ... } ?> 

DAO的例子

 dsn = '(type):host=(host);dbname=(database);charset=UTF8'; $this->dsn = str_replace('(type)', $configuration['type'], $this->dsn); $this->dsn = str_replace('(host)', $configuration['host'], $this->dsn); $this->dsn = str_replace('(database)', $configuration['database'], $this->dsn); $this->options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''); try { $this->databaseHandle = new PDO($this->dsn, $configuration['username'], $configuration['password'], $this->options); } catch (PDOException $e) { ... } } else { ... } } ?> 

如果您在Java中使用Spring框架并且想要在PHP中使用类似的messages.properties解决方案,则可以调整上面提到的相同解决方案。 当然,行为与Java不同,但是,使用Locale类,您可以根据客户端所在的位置创建处理PHP应用程序中的消息/标签的函数。

有了这种基于数组的解决方案,我认为这将帮助您在应用程序中定义所需的值并组织配置数据。 当然,还有很多其他方法可以实现这一点,但我认为解决方案是有效的。

在PHP .ini文件中提供几乎相同的function。 有一些简单的方法可以从这些文件中读取常量。

此外,大多数PHP框架使用扩展名为.php的配置文件来实现它。

例如在cake php中我们有Configure类,它提供类似Configure :: read(’VarName’)和Configure :: write(’VarName’,VarValue);

一旦写入,可以在文件包含范围内访问。