bernhard Posted October 7, 2014 Share Posted October 7, 2014 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 More sharing options...
MindFull Posted October 21, 2014 Share Posted October 21, 2014 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 2 Link to comment Share on other sites More sharing options...
MindFull Posted October 21, 2014 Share Posted October 21, 2014 Not sure if you actually need a deep copy with what you're doing but you can also get a deep copy by overriding the __clone method in PHP within a class of your own. See here for details http://php.net/manual/en/language.oop5.cloning.php 1 Link to comment Share on other sites More sharing options...
bernhard Posted October 21, 2014 Author Share Posted October 21, 2014 thank you mindfull! Link to comment Share on other sites More sharing options...
MindFull Posted October 21, 2014 Share Posted October 21, 2014 Anytime! Link to comment Share on other sites More sharing options...
horst Posted October 21, 2014 Share Posted October 21, 2014 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 4 Link to comment Share on other sites More sharing options...
Recommended Posts