2012-05-12 7 views
1

(オブジェクトの配列を保持する)オブジェクトの配列からメソッドを呼び出すにはどうすればよいですか。私は読む:Get array with results of object method on each item in an array of objects in PHPがそれを得ることができませんでした。オブジェクトの配列の呼び出し方法?

私のテストコードです:最初のオブジェクトは属性を保持し、オブジェクトは複数の属性のレコードを保持します。 > _

あなたはいつもE_ALLとにはdisplay_errorsに設定エラー報告でコードを開発する必要がある属性 -

/*--------------------------------- */ 
class SqliteAttribute { 

    private $_fieldname = ''; 
    private $_fieldvalue = ''; 
    private $_type = 'TEXT'; 
    private $_key = true; 

    function __construct($fieldname, $fieldvalue, $text, $key) { 
     $this->_fieldname = $fieldname; 
     $this->_fieldvalue = $fieldvalue; 
     $this->_text  = $text; 
     $this->_key  = $key; 
    } 

    function AsArray() { 
     $tempArray = array('fieldname'  => $this->_fieldname, 
          'fieldvalue' => $this->_fieldvalue, 
          'type'   => $this->_type, 
          'key'   => $this->_key 
     ); 
     return $tempArray; 
    } 
} 

/*--------------------------------- */ 
class SqliteRecord { 

    private $_attributes = array(); 

    function __construct() { 
    } 

    function AddAttribute($fieldname, $fieldvalue, $text, $key) { 
     $attribute   = new SqliteAttribute($fieldname, $fieldvalue, $text, $key); 
     $this->attributes[] = $attribute; 
     var_dump($this->_attributes); // shows it! 
    } 

    function AsArray() { 
     $temp_array = array(); 
     var_dump($this->_attributes); // shows nothing 
     foreach ($this->_attributes as $key => $value) { 
      $temp_array[] = $value->AsArray(); 
     } 
     return $temp_array; 
    } 

} 

と私はこの

function updateFiles($files, $rootpath) { 
    $recordset = new SqliteRecordSet; 
    foreach ($files as $file) { 
     $record = new SqliteRecord; 
     $record->AddAttribute('Path', $file[0], 'TEXT', true); 

     print_r($record->AsArray()); // shows nothing  
    } 

    $recordset->insertIfNotExist_index(); 
} 

答えて

1

ます$ this->のようにそれを呼び出すが、これは$対属性。 PHPはここにあなたの間違いをあなたに通知していたでしょう。

+0

* argh *あなたは正しい* argh argh argh *(私は間違った方向でこの検索に数時間を費やすことに注意してください) – edelwater

+0

p.s.私はerror_reporting(E_ALL)を持っています。 ini_set( 'display_errors'、 '1'); – edelwater

+0

が設定されていないか、設定を解除した他のコードがある、またはPHPのエラーハンドラを操作したことがあります。 – goat

関連する問題