Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • php class vars auto instantiation using (PDO) ?

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 59
    Answer it

    I'm creating an admin class and i need to know how to auto instantiating the class vars

     

        class Admin{
    
    
            public $admin_id ;
            public $admin_name ;
            public $admin_email ;
            public $admin_type ;
            public $admin_password ;
    
            // methods goes here
    
    
        }
    
            $admin = new Admin();
            $class_vars = get_class_vars(get_class($admin));
    
            foreach ($class_vars as $name => $value) {
                     echo "$name : $value<br />";
                 }

     

    in some of lynda tutorial (php beyond the basic ) you have to create couple of methods so you can instantiation the class vars using mysqli

     

    private static function instantiation($database_row){
    
            $object = new self ; 
    
            foreach($database_row as $attribute => $value){
    
                if($object->has_attribute($attribute)){
    
                    $object->$attribute = $value ;
    
                }
    
            }
            return $object;
    
        }
    
    private function  has_attribute($attribute){
    
    $object_properties = get_object_vars($this);
    return array_key_exists($attribute,$object_properties);
    }

     

    but i need to use the pdo instead of mysqli

 1 Answer(s)

  • Hello Nasyia,

    I just reviewed your code. You can use same code with PDO connection. You need to study the PDO syntax and functions. Kindly check our blogs on PDO.
    http://findnerd.com/account/#url=/list/view/PHP-PDO/18706/
    http://findnerd.com/account/#url=/list/view/How-to-connect-database-using-PDO-in-php/8325/
    http://findnerd.com/account/#url=/list/view/How-to-Authenticate-user-in-PHP-using-PDO/9289/
    http://findnerd.com/account/#url=/list/view/How-to-insert-multiple-records-into-MySQL-Using-MySQLiPDO/13510/

    Thank you

    Deepak Verma
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: