diff --git a/core/includes/install.inc b/core/includes/install.inc
index 6f61503..ebcab90 100644
--- a/core/includes/install.inc
+++ b/core/includes/install.inc
@@ -165,6 +165,49 @@ 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) {
+  clearstatcache(TRUE, $filepath);
+
+  try {
+    if (function_exists('opcache_invalidate')) {
+      $timestamps = ini_get('opcache.validate_timestamps');
+      if (!$timestamps || ($timestamps && ini_get('opcache.revalidate_freq') != 0)) {
+        opcache_invalidate($filepath);
+      }
+    }
+    if (function_exists('apc_delete_file') && ini_get('apc.stat') == 0) {
+      apc_delete_file($filepath);
+    }
+    if (function_exists('wincache_refresh_if_changed')) {
+      if (ini_get('wincache.fcndetect') == 0 || ini_get('wincache.chkinterval') == 0) {
+        wincache_refresh_if_changed(array($filepath));
+      }
+    }
+    if (function_exists('xcache_clear_cache')) {
+      // Use @ error suppression.
+      @xcache_clear_cache(XC_TYPE_PHP);
+    }
+    if (function_exists('eaccelerator_clear')) {
+      // Use @ error suppression.
+      @eaccelerator_clear();
+    }
+  }
+  catch (Exception $e) {
+    throw new Exception(t('Failed to clear the opcode cache for %settings. 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('%settings' => $settings_file)));
+  }
+}
+
+/**
  * Replaces values in settings.php with values in the submitted array.
  *
  * This function replaces values in place if possible, even for
@@ -324,6 +367,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 {
