Q:

php extending mysqli and preparing statements

So I copied a class from code on another stackoverflow post:
class db extends mysqli{
  protected static $instance;
  protected static $options = array();

  private function __construct() { $o = self::$options;
    // turn of error reporting
    mysqli_report(MYSQLI_REPORT_OFF);

    // connect to database
    @parent::__construct(isset($o['host'])   ? $o['host']   : 'localhost',
      isset($o['user'])   ? $o['user']   : 'root',
      isset($o['pass'])   ? $o['pass']   : 'password',
      isset($o['dbname']) ? $o['dbname'] : 'database',
      isset($o['port'])   ? $o['port']   : 3306,
      isset($o['sock'])   ? $o['sock']   : false );
    if( mysqli_connect_errno() ) {
      throw new exception(mysqli_connect_error(), mysqli_connect_errno());
    }
  } 

  public static function getInstance(){
    if(!self::$instance ){
      self::$instance = new self();
    }
    return self::$instance;
  }

  /** OTHER METHOD NOT SHOWN FOR CLARITY **/
}

This class works just fine, and I am able to prepare statements as follows:
$sql = db::getInstance();
$sql->query($dropExampleTable);
$sql->query($ExampleTable);

$title = 'this is an example';

if($stmt = $sql->prepare($exampleQuery)){
  $stmt->bind_param("s",$title);   

  if(!$stmt->execute()){
    echo "example: successful <br />";
  }
}else{
  echo "example: failed <br />";
}

This will successfully add an item into the database. Now this is where my question begins: Why is it that if I add the following method to my db class I am not able to create and successfully execute a statement?
  public function func(){    

    if($stmt = self::$instance->prepare($exampleQuery)){    
      $name = 'func';   
      $stmt->bind_param("s",$name);    
      if(!$stmt->execute()){   
        echo "example: successful <br />";  
      }
    }else{  
      echo "example: failed <br />";
    }                                                  
  }

Even if I pass the sql object created with getInstance() as a reference, I am still unable to successfully prepare a statement:
  public function func2(&$sql){    

    if($stmt = $sql->prepare($exampleQuery)){    
      $name = 'func2';   
      $stmt->bind_param("s",$name);    
      if(!$stmt->execute()){   
        echo "example: successful <br />";  
      }
    }else{  
      echo "example: failed <br />";
    }                                                  
  }

BUT I am able to create a statement as I did at the top of the post, and pass it as a reference and execute that statement in a method I create inside the db class:
  public function func3(&$stmt){    

    $name = 'func3';   
    $stmt->bind_param("s",$name);    
    if(!$stmt->execute()){   
      echo "example: successful <br />";  
    }
  }                                                  

But I don't want to have to pass a prepared statement to my class; seems to defeat the purpose of it. My ultimate goal is to be able to pass an array of items that need to be added to the database to my db instance and prepare the statement inside a method.

A:

The idea of a singleton object is to have only one instance of an object, that's why the constructor method is private. Which means that you cannot call any non-static methods, in your case you func function is actually not a static one - that needs to be changed. Then, in that method, it seems that you have place the else statement on the first if, if you really want to see if statement was executed correctly, you should move it to the other if. Here is a slightly updated version of your method, it works fine:
public static function func($exampleQuery)
{
    if ($stmt = self::$instance->prepare($exampleQuery)) {    
        $name = 'some name'; // search criteria
        $stmt->bind_param("s", $name);
        if ($stmt->execute()) {   
            echo "example: successful <br />";  
        } else {
            echo "example: failed <br />";
        }
    }
}

Usage
$db = db::getInstance();
$db->func('SELECT `id` FROM `table` WHERE `name` = ?');
// prints out 'example: successful '

