title: Require a file dynamically in php
body: <p>I'm racking my brain here and after several attempts and research I was forced to ask for assistance from you. </p>
  
  <p>I have two files as follows: </p>
  
  <pre><code>- SITE/conf1g.php 
  - SITE / library / Mysqli.class.php 
  </code></pre>
  
  <p>The conf1g.php file contains all site settings file and Mysqli is what makes the connection to the database using the variables defined in the file conf1g.php </p>
  
  <p>But I use the Mysqli.class.php at multiple locations using require_once. How do I include the file in conf1g.php __ construct the class? (The class is in Mysqli.class.php file) </p>
  
  <p>The best I could get was this:</p>
  
  <pre><code>&lt;?php
  function __construct() {
  
   $path = $_SERVER['SCRIPT_FILENAME'];
   $pa = $path_parts = pathinfo($path);
   $file = str_replace($path_parts['basename'],'',$_SERVER['SCRIPT_FILENAME']);
   $file = str_replace($file,'',dirname(__FILE__));
   $file = str_replace('library','conf1g.php',$file);
   require_once($file);
  
   //PDO CONNECTION CODE HERE
  
  }
  ?&gt;
  </code></pre>
  
  <p>But the error if I use subfolders like: </p>
  
  <pre><code>-/var/www/folder/sub_folder/userhere.php 
  </code></pre>
  
  <p>And the site is at: </p>
  
  <pre><code>-/var/www/SITE /
  </code></pre>
  
  <p>Any idea how to include this file in a dynamic way? Or any solution?</p>