WordPress singleton instance practice

Today we will discuss the Singleton pattern, the most controversial creation pattern.
A Singleton is a global point of access for a resource that must have only one instance in the whole application: database connections, http streams, and similar objects are the typical examples of such a class.

This behavior is implemented trough a static method which returns a lazy-created instance, and by rendering the constructor private to avoid creation of an instance from outside the class code.

This is a simple example of a singleton implementation:

<?php 
class PR_SINEXAMPLE { 	

       /** 	 
       * Placeholder method 	 
       */ 	
       private function __construct() {           
          $this->setup();
       }

	/**
	 * Get a singleton instance of the class
	 */
	public static function factory() {
		static $instance = false;
		if ( ! $instance ) {
			$instance = new self();
		}
		return $instance;
	}

        /**
	 * Setup actions and filters
	 */
	public function setup() {
        }

}
global $pr_singleton;
$pr_singleton = PR_SINEXAMPLE::factory();

The problems of a Singleton class are the same of its non object-oriented equivalent: a global variable. The Api of Clients of the Singleton lies, because they do not declare in any signature that they are taking advantage of the Singleton’s capabilities; moreover, global mutable state is introduced and encapsulation is disregarded.

Calling stored procedure in wordpress using wpdb

Today I was get stuck while calling stored procedure in wordpress. Neither $wpdb->get_results nor $wpdb->get_results was working. I got the point somrthing was went wrong. The output of  $wpdb->show_errors() is nothing so there was not an error.

This is a create statement of my procedure I’m tried to called it using $wpdb

CREATE PROCEDURE posts_count(OUT @total_posts INT)
BEGIN
SELECT COUNT(*)  INTO total_posts FROM wp_posts;
END//

Okay so I get to knew while try and test try and test

$result = $wpdb->query("CALL posts_count(@total_posts)");
$result = $wpdb->get_results("SELECT @total_posts");
var_dump( $result );

So finally it working. It is returning me a correct output

array (size=1)
0 =>
object(stdClass)[361]
public '@total_posts' => string '19' (length=2)