Hi.
I have an AJAX code which sends requests to multiple .php files in the server. All the files (that I send request) are using the same Db class and I think using sigleton pattern will help me out. But now I doubt that everytime I send request it creates a whole new objectn (instead of grabing the object hold by $instance static variable) . Is it correct ? And if so how can I make an instance only once . By the way on the top of all php files that I am sending request I type

require_once("includes/Db.php");

$db=Db::getSingletone();

And below is my Db class

require_once("includes/config.php");  // Configuration
            
    class Db{
            public $connection;
            public $selected_db;
            private static $instance;
            
            public static function getSingletone(){
                                   if(!self::$instance instanceof self){
                                           self::$instance=new self();
                                   }       
                                   return self::$instance;
            }
         
            public function __construct(){
                                                                                                                                                                                                                             
                                $this->connection=mysql_connect(DB_HOST,DB_USER,DB_PASS);               // 1 . Creating a connection  
                                if(!$this->connection){
                                    die("Couldnt create a database connection ".  mysql_error());
                                }   
                                $this->selected_db=mysql_select_db(DB_NAME,$this->connection);          // 2. Selects a database
                                if(!$this->selected_db){
                                    die("Could not select the database: ".mysql_error());
                                }//  End "if"   for selecting a database
                                    
                            } 
                            
                            
            public function insert($user,$time,$message){
                                $query="INSERT INTO chat(user,time,message) VALUES('{$user}','{$time}','{$message}')";
                                $result=mysql_query($query,$this->connection);
                                 if(!$result){
                                   die("Could Not Insert ".mysql_error());
                                 }
                
                            }// End  "insert()"  method
            
            public function execute($query){
                 
                                $result=mysql_query($query,$this->connection);
                                if(!$result){
                                    die("Database query failed".mysql_error());
                                }
                                     return $result;
                            } // End "execute()" method
                            
            public function row_number($query){
                                $result=$this->execute($query);
                                $result=mysql_num_rows($result);
                                return $result;
            }                
            
            public function delete_rows($table){
                                $query="DELETE FROM '{$table}'  WHERE id=7";
                                $result=mysql_query($query,$this->connection);       
                                 if(!$result){
                                    die("Could NOT delete".mysql_error());
                                 }
                
                            } //  End "delete_rows_by_ids()"  method
                         
     
                            
            public function __destruct(){           // __destruct()
                                mysql_close($this->connection);
                                
                            } // Destroys created object
                          
                            
            } // End the "db"   class

HTTP requests in themselves are stateless. The only way to persist data/variables between requests is to persist them in a file (database, session, cookie).

The singleton pattern will only work during the same request. So if during a single AJAX request, you get a database instance twice using getSingleton, it will return the same object.

Over two separate AJAX requests, you will get two separate database instances, because as soon as a request finishes, all variables go out of scope.

R.

Thank you. Understood!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.