将数组值分配给单独的类值

时间:2013-04-15 06:29:59

标签: php arrays oop class

我有一个configuration.php作为一个单独的文件.IT contat plowFactory类有11个变量。我需要从array中分配它们的值。$ ConfigData在另一个文件中。我该怎么做?

CLASS

   <?php 
     class PardFactory { 
     public $TITLE = 'Sri lanka Accommodation';
     public $ONLINE = 'Site Online';
     public $OFFLINE =  '';
     public $EMAIL =  '';
     public $METADESCRIPTION =   '';
     public $METAKEYWORDS =  '';
     public $COPYRIGHT =  '';
     public $HOST =  '';
     public $DATABASE =  '';
     public $USERNAME =  '';
     public $PASSWORD =  '';
     } 
     ?>

ARRAY

$ConfigData = array($adminConfig->TITLE,
 $adminConfig->ONLINE,
 $adminConfig->OFFLINE,
 $adminConfig->EMAIL,
 $adminConfig->METADESCRIPTION,
 $adminConfig->METAKEYWORDS,
 $adminConfig->COPYRIGHT,
 $adminConfig->HOST,
 $adminConfig->DATABASE,
 $adminConfig->USERNAME,
 $adminConfig->PASSWORD);

我想将数组值传递给类值。

另外,我需要在该configuration.php中只有上面的pardFactory类。如果我这样做,我需要先将pardFactory类包含在configuration.php中。我需要像joomla configuration.php文件这样的文件

2 个答案:

答案 0 :(得分:0)

这样做会创建一个类文件的对象:

$obj = new PardFactory($ConfigData);

并创建一个构造函数

function __construct($config) {
    $this->TITLE = $config->TITLE;
    $this->ONLINE = $config->ONLINE;
}

等等。

答案 1 :(得分:0)

尝试这样的事情

  $obj = new PardFactory($ConfigData);

class PardFactory {
public function __construct(Array $config=array()){
  foreach($config as $key => $value){
    $this->{$key} = $value;
  }
}
}