Jump to content

multisite - get config of other sites


bernhard
 Share

Recommended Posts

i have a multisite setup (option #1) and wanted to get some config values (dbname + dbpass) of other instances.

i tried this: 

$config_tmp = $config; // store current config to config_tmp
echo $config_tmp->dbName . '<br>'; // says current_db_name
include("../../site-xy/config.php");
echo $config->dbName . '<br>'; // says remote_db_name
$config = $config_tmp;
echo $config->dbName . '<br>'; // says remote_db_name -----------> why?

why is the $config at the end still having the value of the included config?

Link to comment
Share on other sites

  • 2 weeks later...

Because PHP will assign an object pointer to a variable when trying to use the = operator to duplicate an object. It's passed by reference, so to say, though I don't like to define it like that.

$config_tmp = $config; // $config_tmp points to $config
echo $config_tmp->dbName . '<br>'; // says current_db_name
include("../../site-xy/config.php"); // you overwrite $config here, but $config_tmp still points to it
echo $config->dbName . '<br>'; // says remote_db_name, because you overwrote $config with the include
$config = $config_tmp; // $config now points to $config_tmp, which still points at $config, which was overwritten with new values from the include ($config points to an object pointer that points to an object)
echo $config->dbName . '<br>'; // says remote_db_name -----------> why? -- See above

What you need to do is either clone $config, which will give you a shallow copy or serialize then deserialize the object to achieve a deep copy. ie

$config_tmp = unserialize(serialize($config)); // Getting deep copy of $config 
   OR
$config_tmp = clone($config); // Getting shallow copy of $config

echo $config_tmp->dbName . '<br/>'; // says current_db_name
include("../../site-xy/config.php"); // original $config gets overwritten
echo $config->dbName . '<br/>'; // says remote_db_name
$config = $config_tmp;  // $config is now pointing to the deep/shallow copy you made
echo $config->dbName . '<br/>'; // should say current config_db_name now
  • Like 2
Link to comment
Share on other sites

Another solution could be to wrap a function around the include for the remote file. This way it gets opened in its own scope and doesn't override your $config object:

function readRemoteConfig($pathToFile) {
    $config = new stdClass();  // it is enough to use a StandardObject here, - without that, PHP will raise a 'notice' I believe.
    include($pathToFile);
    return $config;
}

$remoteConf1 = readRemoteConfig("path/to/remote1/site/config.php");
$remoteConf2 = readRemoteConfig("path/to/remote2/site/config.php");
$remoteConf3 = readRemoteConfig("path/to/remote3/site/config.php");

// and your (local) $config isn't affected or touched in any way
  • Like 4
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...