title: How do I fix 'Call to a member function real_escape_string() on null'?
body: <p>I am trying to make the switch from procedural PHP to OO PHP.</p>
  
  <p>I have created a Db class that looks like this:</p>
  
  <pre><code>   class Db {
  
         protected static $connection;
  
         /*
         Attempt to connect to database. 
  
         @Return bool false on failure, mysqli object instance on success
  
         */
  
         public function connect() {
  
             if(!isset(self::$connection)) {
  
                  //Parse DB info from configuration file
                  $config = parse_ini_file('../validconfigpath/config.ini');
  
                  //Attempt to connect to the datbase
                  self::$connection = new mysqli($config['dbhost'], $config['dbuser'], $config['dbpass'], $config['dbname']);
  
  
                  if(self::$connection === false) {
                      //Database connection unsuccessful, how to handle?
                      echo "Error connecting to database.&lt;br /&gt;";
                      return false;
                  }
  
                  return self::$connection;
             }
         }
  
         /*
          * Query the database
          * @param $query The sql string
          * @return mixed The result of the mysqli::query() function
          */
  
         public function query($query) {
             //Connect to the database
             $connection = $this -&gt; connect();
  
             //Query the database
             $result = $connection -&gt; query($query);
  
             return $result;
         }
  
         /*
          * Fetch rows from the database (SELECT query)
          * @param $query The query string
          * @return bool false on failure / array Database rows on success
          * 
          */
  
         public function select($query) {
             $rows = array();
             $result = $this -&gt; query($query);
  
             if ($result === false) {
                 return false;
             }
  
             while($row = $result -&gt; fetch_assoc()) {
                 $rows[] = $row;
             }
  
             return $rows;
  
         }
  
         /*
          * Fetch last inserted ID from the database
          * @return ID of last inserted record using this connection, or 0 if no INSERT or UPDATE statements were sent
          */
  
         public function inserted_id() {
  
             $connection = $this -&gt; connect();
  
             return $connection -&gt; insert_id;
  
         }
  
         public function quote($value) {
  
             $connection = $this -&gt; connect();
  
             return "'" . $connection -&gt; real_escape_string($value) . "'";
         }
  
  
     }
  </code></pre>
  
  <p>I am trying to test the <code>quote</code> function, by using this code:</p>
  
  <pre><code>include_once 'Classes/DB.php';
  
  $db = new Db();
  
  $firstName = $db -&gt; quote("First");
  echo $firstName. "&lt;br /&gt;";
  
  
  $lastName = $db -&gt; quote("Last");
  echo $lastName. "&lt;br /&gt;";
  </code></pre>
  
  <p>I get my expected output for <code>$firstName</code> like so:</p>
  
  <pre><code>'First'
  </code></pre>
  
  <p>But for my second output for <code>$lastName</code> I get:</p>
  
  <pre><code>Fatal error: Uncaught Error: Call to a member function real_escape_string() on null in ..\Classes\DB.php:91 Stack trace: #0 ..\index.php(11): Db-&gt;quote('Last') #1 {main} thrown in ..\Classes\DB.php on line 91
  </code></pre>
  
  <p>As best I can tell something is happening to <code>$connection</code> but I can't figure out what is wrong with <code>connect()</code>.</p>
  
  <p>Also I got to thinking is it best to just create a connection in <code>__construct</code>?</p>