[英]Edit the array and save the file as a new php file
There is a file named default.Db.php
just containing:有一个名为
default.Db.php
的文件只包含:
<?php
// OLD FILE
function get_DbConfig(){
$config = array (
'source' => 'array',
'host' => 'DATABASE_HOST',
'port' => 'DATABASE_PORT',
'username' => 'DATABASE_USER',
'password' => 'DATABASE_PASSWORD',
'database' => 'DATABASE_NAME'
);
return $config;
}
require_once './../Common/php/face.php';
?>
What is way to replace $config
inside get_dbConfig
with my own array?用我自己的数组替换
get_dbConfig
$config
方法是什么? For example, an array where each key has a specific value.例如,一个数组,其中每个键都有一个特定的值。 After replacing the values inside this array, I will rename the file to
Db.php
替换此数组中的值后,我将文件重命名为
Db.php
<?php
// NEW FILE
function get_DbConfig(){
$config = array (
'source' => 'array',
'host' => 'localhost',
'port' => '3306',
'username' => 'foo',
'password' => 'bar',
'database' => 'foobar'
);
return $config;
}
require_once __DIR__.'./../Common/php/OperateDB/DbMgrInterface.php';
?>
First you have to pass argument in your function.首先,您必须在函数中传递参数。 after that when you call that function, at that time you have to pass your array in that and assign array value with assign variable.
之后,当您调用该函数时,那时您必须将数组传递给该数组并使用分配变量分配数组值。
<?php
function get_DbConfig($configdata){
$config = array (
'source' => $configdata ['array'],
'host' => $configdata['DATABASE_HOST'],
'port' => $configdata['DATABASE_PORT'],
'username' => $configdata['DATABASE_USER'],
'password' => $configdata['DATABASE_PASSWORD'],
'database' => $configdata['DATABASE_NAME']
);
return $config;
}
require_once './../Common/php/face.php';
?>
I don't know why you want to do this way.我不知道你为什么要这样做。 There are better methods to achieve this.
有更好的方法来实现这一点。 However, you could do the following:
但是,您可以执行以下操作:
try {
// Read the whole file into memory
$fileStr = file_get_contents('../default.Db.php');
// Replace each string with a valid value
$fileStr = str_replace('DATABASE_HOST', HOST, $fileStr);
$fileStr = str_replace('DATABASE_USER', USER, $fileStr);
$fileStr = str_replace('DATABASE_PASSWORD', PASSWORD, $fileStr);
$fileStr = str_replace('DATABASE_NAME', DB_NAME, $fileStr);
$fileStr = str_replace('DATABASE_PORT', PORT, $fileStr);
// Write the modified content
file_put_contents("../default.Db.php", $fileStr);
// Rename default.Db.php to Db.php
$isRenamed = rename('../default.Db.php','../Db.php');
if($isRenamed) {
require_once './../Db.php';
}
}catch(Exception $exc) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.