Page 1 of 1

problem with passing configuration data to database class.

Posted: Tue Aug 05, 2008 4:30 pm
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.

Re: problem with passing configuration data to database class.

Posted: Tue Aug 05, 2008 4:44 pm
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.

Re: problem with passing configuration data to database class.

Posted: Tue Aug 05, 2008 4:52 pm
by haosmark
Thanks, i changed it, now it works.