problem with passing configuration data to database class.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
haosmark
Forum Newbie
Posts: 10
Joined: Wed Mar 19, 2008 12:10 pm

problem with passing configuration data to database class.

Post by haosmark »

For some reason i can't pass configuration data to my database class:

Code: Select all

    require('Zend/Db/Adapter/Pdo/Mysql.php');
 
    
    class Database {
        private $dbh;
        
        public static function connect() {
            try {
                $dbh = Zend_Db::factory($config->database);
            }
            catch(Zend_Exception $e) {
                echo $e->getMessage();
                exit();
            }
            
        return $dbh;
        }
    }
The above code generated an error:
Adapter name must be specified in a string
and the reason for that is that $config->database is empty.
Now if i type in:

Code: Select all

    require('Zend/Db/Adapter/Pdo/Mysql.php');
    print_r($config);
    
    class Database {
        private $dbh;
        
        public static function connect() {
            try {
                $dbh = Zend_Db::factory($config->database);
            }
            catch(Zend_Exception $e) {
                echo $e->getMessage();
                exit();
            }
            
        return $dbh;
        }
    }
then print_r($config); will generate the following output:
Zend_Config Object ( [_allowModifications:protected] => [_index:protected] => 0 [_count:protected] => 3 [_data:protected] => Array ( [general] => Zend_Config Object ( [_allowModifications:protected] => [_index:protected] => 0 [_count:protected] => 1 [_data:protected] => Array ( [site_title] => Movie Talk ) [_loadedSection:protected] => [_extends:protected] => Array ( ) ) [database] => Zend_Config Object ( [_allowModifications:protected] => [_index:protected] => 0 [_count:protected] => 2 [_data:protected] => Array ( [adapter] => Mysqli [params] => Zend_Config Object ( [_allowModifications:protected] => [_index:protected] => 0 [_count:protected] => 4 [_data:protected] => Array ( [host] => localhost [username] => root [password] => [dbname] => movietalk ) [_loadedSection:protected] => [_extends:protected] => Array ( ) ) ) [_loadedSection:protected] => [_extends:protected] => Array ( ) ) [dir] => Zend_Config Object ( [_allowModifications:protected] => [_index:protected] => 0 [_count:protected] => 4 [_data:protected] => Array ( [root] => E:/PortableApps/WOS/www/MovieTalk_2/ [assets] => E:/PortableApps/WOS/www/MovieTalk_2/public/assets/ [business] => Zend_Config Object ( [_allowModifications:protected] => [_index:protected] => 0 [_count:protected] => 1 [_data:protected] => Array ( [database] => E:/PortableApps/WOS/www/MovieTalk_2/business/Database.class.php ) [_loadedSection:protected] => [_extends:protected] => Array ( ) ) [public] => E:/PortableApps/WOS/www/MovieTalk_2/public/ ) [_loadedSection:protected] => [_extends:protected] => Array ( ) ) ) [_loadedSection:protected] => [_extends:protected] => Array ( ) ) Adapter name must be specified in a string
but if i put "print_r" inside connect method then "$config" becomes empty.
Why is this happening and how do i fix it?

Thanks.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: problem with passing configuration data to database class.

Post by RobertGonzalez »

You are having a scope problem. Things that live in the script scope do not live in the class or function scope. You need to pass that value to your class so your class knows it lives.
haosmark
Forum Newbie
Posts: 10
Joined: Wed Mar 19, 2008 12:10 pm

Re: problem with passing configuration data to database class.

Post by haosmark »

Thanks, i changed it, now it works.
Post Reply