ugrás a tartalomhoz

lehet modositani tomb tipusu statikus valtozot?

carstepPCE · 2008. Dec. 16. (K), 12.28
Udv Mindenkinek,

egy erdekes dilemmaba futottam bele 5.2.6-s PHP-nal, szerettem volna kihasznalni a 5-s php adta lehetosegeket es statikus osztalyt szeretnek letrehozni adatbazis kapcsolatok kezelesehez. Egy script futasaban akar tobb adatbazishoz is kapcsolodhatok.Gondoltam letrehozok egy statikus tombot nekik,de amikor tesztelni akartam, azt a hibauzenetet kaptam, hogy Arrays are not allowed in class constants. Hat akarhogy neztem a php dokumentaciojat sehol sem talaltam arra utasitast, hogy statikus valtozo nem lehet tomb tipusu.:

Teszt kod, amit hasznaltam:
  1. require_once"config.php" );  
  2. require_once"DBConnection.php" );  
  3.   
  4. DBConnection::init();  
  5.   
  6. DBConnection::loadConnection('transaction');  
  7. DBConnection::loadConnection('filters');  
  8.   
  9. var_dump( DBConnection::$_db );  
  10. DBConnection::destroy();  
  1. class DBConnection {  
  2.   
  3.     static $_db = array();  
  4.   
  5.     private static function init(){  
  6.         if ( !class_exists'staticDBConfig' ) ) {  
  7.             define("DS",DIRECTORY_SEPARATOR);  
  8.             $filename = 'incl' . self::DS . 'config.php';  
  9.             if ( file_exists$filename ) ) {  
  10.                 include$filename );  
  11.             } else {  
  12.                 die'No config file exists' );  
  13.             }  
  14.         }  
  15.     }  
  16.   
  17.     public static function addConnection( $connectionname ) {  
  18.         if ( ! emptyempty$connectionname ) && ! isset( self::$_db[$connectionname] ) ) {  
  19.             if ( ! class_exists(staticDBConfig) ) {  
  20.                 staticDBConfig::getConnection($connectionname);  
  21.                 self::$_db[$connectionname] = self::mysql_pdo_connect(staticDBConfig::$settings);  
  22.             }  
  23.         }  
  24.   
  25.         return self::$_db[$connectionname];  
  26.     }  
  27.   
  28.     public static function loadConnection( $connectionname ) {  
  29.         if ( !isset( self::$_db[$connectionname] ) ) {  
  30.             self::addConnection($connectionname);  
  31.         }  
  32.   
  33.         return self::$_db[$connectionname];  
  34.     }  
  35.   
  36.     /** 
  37.      * PDO connection method. We pass a string parameter to select which database we want to connect to, 
  38.      * We load based on this parameter the configuration properties for the connection. 
  39.      * If port property is define we use it to be specific to this. 
  40.      * 
  41.      * @param string $connectionprops, which database to connect to 
  42.      * @return PDO connectin resource of false 
  43.      */  
  44.     public static function mysql_pdo_connect($connectionprops) {  
  45.         $config = $connectionprops;  
  46.         $conn = NULL;  
  47.         try {  
  48.             if (isset ( $config ['port'] ) && ! emptyempty ( $config ['port'] ))  
  49.                 $conn = new PDO ( 'mysql:host=' . $config ['host'] . ';port=' . $config ['port'] . ';dbname=' . $config ['database'] . ';'$config ['login'], $config ['password'] );  
  50.             else  
  51.                 $conn = new PDO ( 'mysql:host=' . $config ['host'] . ';dbname=' . $config ['database'] . ';'$config ['login'], $config ['password'] );  
  52.         } catch ( PDOException $e ) {  
  53.             die ( "Could not connect to db!" );  
  54.             // ( $e->getMessage (), 1 );  
  55.         }  
  56.   
  57.         return $conn;  
  58.     }  
  59.   
  60.     public static function destroy(){  
  61.         foreach( self::$_db as $key => $item ) {  
  62.             self::$_db[$key] = null;  
  63.             unset( self::$_db[$key]);  
  64.         }  
  65.     }  
  66. }  
  1. class staticDBConfig {  
  2.     const _default  = array ('driver' => 'mysql''persistent' => false, 'host' => 'localhost''login' => '****''password' => '*****''database' => '****_full''port' => '3307''prefix' => '' );  
  3.     const _default2 = array ('driver' => 'mysql''persistent' => false, 'host' => 'localhost''login' => '****''password' => '*****''database' => '*****''port' => '3307''prefix' => '' );  
  4.     const _default3 = array ('driver' => 'mysql''persistent' => false, 'host' => 'localhost''login' => '****''password' => '*****''database' => '****''port' => '3307''prefix' => '' );  
  5.     const _default4 = array ('driver' => 'mysql''persistent' => false, 'host' => 'localhost''login' => '***''password' => '***''database' => '*****''port' => '3307''prefix' => '');  
  6.     const _default5 = array ('driver' => 'mysql''persistent' => false, 'host' => 'localhost''login' => '****''password' => '****''database' => '******_main''port' => '3307''prefix' => '');  
  7.   
  8.     static public $settings;  
  9.   
  10.     public static function getConnection( $name ) {  
  11.         switch ( $name ) {  
  12.             case "service"      : self::$settings = self::_default2; break;  
  13.             case "service3"     : self::$settings = self::_default3; break;  
  14.             case "transaction"  : self::$settings = self::_default4; break;  
  15.             case "filters"      : self::$settings = self::_default5; break;  
  16.             default             : self::$settings = self::_default;  
  17.         }  
  18.     }  
  19.   
  20.     public static function getDetail( $detailProperty ) {  
  21.         if ( emptyempty$detailProperty ) ) {  
  22.             return self::$settings;  
  23.         } else {  
  24.             if ( isset( self::$settings[$detailProperty] ) ) {  
  25.                 return self::$settings[$detailProperty];  
  26.             } else {  
  27.                 return false;  
  28.             }  
  29.         }  
  30.     }  
  31. }  
A segitsegeteket kerem, hatha valamit nagyon elneztem a statikus valtozokkal kapcsolatban?

koszi
-cs-
Sanyi
 
1

maga a konstans nem lehet tömb

gex · 2008. Dec. 16. (K), 12.52
Only scalar data (boolean, integer, float and string) can be contained in constants.

forrás
2

igazabol ez nem valasz a kerdesemre

carstepPCE · 2008. Dec. 16. (K), 12.58
vagy ezek szerint a statikus valtozo konstansnak szamit PHP-ban, akkor a singleton pattern sem mukodne?

-cs-
Sanyi
3

igazad van!

carstepPCE · 2008. Dec. 16. (K), 13.36
hat elegge reggel van meg, es nem ittam meg a teamat, ugyhogy elnezest mindenkitol, aki megnezte a problememat.

Hat nem rossz fajlt neztem vegig :-)

-cs-
Sanyi