fopen 将变量添加到文件

fopen to add variable to file

我正在为我的产品创建一个安装程序,允许开发人员安装软件。有一个配置 php 文件,我在 class 存在的同一目录中打开它工作正常。

class Package {
    private $_config;
    public function __construct($dsn,$user,$pass) {
        $this->_config = fopen('Config.php', 'w') or die("Unable to find Config file");
        [...]

但是,当我想像这样将连接详细信息添加到文件时:

fwrite($this->_config, "$con = ['$dsn','$user','$pass'];");
fclose($this->_config);

配置文件如下所示:

 = ['val', 'val', 'val']; // where val is the actual value of $dsn, $user, $pass

我怎样才能使文件看起来像这样?:

$con = ['val', 'val', 'val'];

从未真正使用过文件来存储信息,只使用过数据库,所以我对这个概念很陌生。

不要使用双引号 --"--,因为它们会将 "$con" 计算为空字符串 试试这个:

fwrite($this->_config, '$con' . " = ['$dsn','$user','$pass'];");