diff --git a/core/includes/install.inc b/core/includes/install.inc
index 6f61503..066884c 100644
--- a/core/includes/install.inc
+++ b/core/includes/install.inc
@@ -165,6 +165,52 @@ function drupal_get_database_types() {
 }
 
 /**
+ * Flushes a PHP file from any active opcode cache.
+ *
+ * If it is not possible to flush an individual file, the whole cache will be
+ * flushed.
+ *
+ * @param string $filepath
+ *   Filepath of PHP file to be flushed.
+ *
+ * @throws Exception
+ */
+function drupal_clear_opcode_cache($filepath) {
+  // This setting can be On/Off or 1/0, but when the setting is Off
+  // the returned value is an empty string instead of 0.
+  $empty_values = array("", 0);
+  clearstatcache(TRUE, $filepath);
+
+  try {
+    if (extension_loaded('Zend OPcache')) {
+      $timestamps = ini_get('opcache.validate_timestamps');
+      if (!$timestamps || ($timestamps && ini_get('opcache.revalidate_freq') != 0)) {
+        opcache_invalidate($filepath);
+      }
+    }
+    if (extension_loaded('apc') && in_array(ini_get('apc.stat'), $empty_values)) {
+      apc_delete_file($filepath);
+    }
+    if (extension_loaded('wincache')) {
+      if (in_array(ini_get('wincache.fcndetect'), $empty_values) || ini_get('wincache.chkinterval') == 0) {
+        wincache_refresh_if_changed(array($filepath));
+      }
+    }
+    if (extension_loaded('XCache') && in_array(ini_get('xcache.stat'), $empty_values)) {
+      for ($i = 0; $i < xcache_count(XC_TYPE_PHP); $i++) {
+        xcache_clear_cache(XC_TYPE_PHP, $i);
+      }
+    }
+    if (extension_loaded('eaccelerator')) {
+      eaccelerator_clear();
+    }
+  }
+  catch (Exception $e) {
+    throw new Exception(t('Failed to clear the opcode cache for %file. For the duration of the installation it is recommended to either disable the opcode cache, or reconfigure it to allow code changes to be seen immediately.', array('%file' => $filepath)));
+  }
+}
+
+/**
  * Replaces values in settings.php with values in the submitted array.
  *
  * This function replaces values in place if possible, even for
@@ -324,6 +370,13 @@ function drupal_rewrite_settings($settings = array(), $settings_file = NULL) {
         $old_settings = Settings::getAll();
         new Settings($settings_settings + $old_settings);
       }
+
+      // Since the existing PHP file is rewritten, ensure that PHP's opcache is
+      // aware of the new content, in case the file is included again or if the
+      // opcode cache doesn't check the script on each request to determine if
+      // it has been modified.
+      // @see \Drupal\simpletest\WebTestBase::setUp()
+      drupal_clear_opcode_cache(DRUPAL_ROOT . '/' . $settings_file);
     }
   }
   else {
