2012-02-09 19 views
0

트위터 클래스를 만들고 해당 클래스의 메서드와 속성을 호출하는 객체를 만들려고합니다. 본질적으로 내가하고있는 일은 트위터 사용자 이름에 대해 데이터베이스를 호출하고 그 결과로 simplexml 요청을하는 것입니다. (코드가 잘 작동하기 때문에 그 부분을 생략했습니다).전체 배열이 반환되지 않음 PHP

배열의 첫 번째 항목 만 반환 될 때 return $this->posts이 반환되는 이유를 알 수 없다는 것을 제외하면 모든 것이 올바르게 작동하는 것처럼 보입니다. return을 제거하면 전체 배열이 반환됩니다. 맨 아래에있는 객체에 print_r을 사용하여 테스트하고 있습니다.

<?php 
    class twitter { 
     public $xml; 
     public $count; 
     public $query; 
     public $result; 
     public $city; 
     public $subcategory; 
     public $screen_name; 
     public $posts; 

     public function arrayTimeline(){ 
      $this->callDb($this->city, $this->subcategory); 
      while($row = mysql_fetch_row($this->result)){ 
       foreach($row as $screen_name){ 
        $this->getUserTimeline($screen_name, $count=2); 
       } 
       foreach($this->xml as $this->status){ 
        return $this->posts[] = array("image"=>(string)$this->status->user->profile_image_url,"name"=>(string)$this->status->name, "username"=>(string)$this->status->user->name, "text"=>(string)$this->status->text, "time"=>strtotime($this->status->created_at)); 
       } 
      } 
     } 


    $test = new twitter; 
    $test->city="phoenix"; 
    $test->subcategory="computers"; 

    $test->arrayTimeline(); 

    print_r($test->posts); 

    ?> 

답변

5

반환 값은 PHP가 현재 호출중인 메서드를 벗어나게하기 때문입니다. 루프에서 반환을 이동하면 전체 배열을 가져옵니다.

public function arrayTimeline(){ 
     $this->callDb($this->city, $this->subcategory); 
     while($row = mysql_fetch_row($this->result)){ 
      foreach($row as $screen_name){ 
       $this->getUserTimeline($screen_name, $count=2); 
      } 
      foreach($this->xml as $this->status){ 
       $this->posts[] = array("image"=>(string)$this->status->user->profile_image_url,"name"=>(string)$this->status->name, "username"=>(string)$this->status->user->name, "text"=>(string)$this->status->text, "time"=>strtotime($this->status->created_at)); 
      } 
     } 

     return $this->posts; 
    }