SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    49
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Question connection class problem

    dear all here my connection class
    PHP Code:
    <?php
    class Conn_DB {
        private static 
    $_cbInstance NULL;
        private function 
    __construct(){}
        public function 
    __clone(){
            
    trigger_error("Error",E_USER_ERROR);
        }
        public function 
    __wakeup(){
            
    trigger_error("Error",E_USER_ERROR);
        }
        public static function 
    getCBConnect(){
            if(!isset(
    self::$_cbInstance)){
                try{
                    
    self::$_cbInstance = new PDO("mysql:host=localhost;dbname=testingDB;charset=UTF-8","username","password");
                }
                catch (
    PDOException $e){
                    die(
    $e->getMessage());
                }
                return 
    self::$_cbInstance;
            }
        }
        public function 
    __destruct() {
        }
    }
    nothing wrong but when i used my select class
    PHP Code:
    <?php
    class SelectDatabase{
        function 
    select($connect,$field,$table){
            
    $query "SELECT ".$field." FROM ".$table;
            
    $stmt $connect->prepare($query);
            
    $stmt->execute();
            return 
    $stmt;
        }
        function 
    selectWhere($connect,$field,$table,$whereclause){
            
    $query "SELECT ".$field." FROM ".$table." ".$whereclause;
            
    $stmt $connect->prepare($query);
            
    $stmt->execute();
            return 
    $stmt;
        }
        public function 
    __destruct(){}
    }
    ?>
    here my function
    PHP Code:
    <?php
        
    function fnc_select($table)
        {
            
    $conn Conn_DB::getCBConnect();
            
    $sel = new SelectDatabase();
            
    $statement $sel->select($conn," * " ,$table);
            return 
    $statement;
        }    
    ?>
    it work actually but the problem is my public function __destruct(){} when i want to use same select class for another in the same page it fatal error because i my connection class was __destruct so any way use 1 connection and 1 select class for whole project?
    Last edited by cpradio; Jan 23, 2013 at 07:06. Reason: Removed username and password

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    49
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    any body else have an idea for this? if i remove __destruct() method then i create sub class and add __destruct at there is it effect to supper class?

  3. #3
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    I'm not sure I understand the issue, you're say that defining a __destruct is causing an issue, but the method is empty? What exactly is the code running in __destruct() that's causing the problem?

    Presumably, though, because you're using a singleton, Conn_DB can never be destructed anyway? Or is that the issue? If it is, it's a flaw in the singleton pattern and one reason that singletons should be avoided.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    49
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    i have no idea about your command but i just want to share any idea my mind because i try to looking for some solution
    thank for your reply
    Regards,
    Heng


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •