2012-10-05 38 views
0

您好我是PHP的新手我想知道如何填充tweets数组以便返回它。如何填充MyObjects的数组PHP

<?php 

class TwitterService { 
private $params = null; 
private $tweets = array(

); 

public function getParams() { 
    return $this->params; 
} 

public function setParams($params) { 
    $this->params = $params; 
} 


public function __construct($params) { 
    $this->setParams($params); 
} 
public function getTweets(){ 
    private $tweet1 = new Tweet(
      id = 253338336415064064, 
      created_at = 'Wed, 03 Oct 2012 03:39:00 +0000', 
      profile_image_url = 'http:\/\/a0.twimg.com\/profile_images\/1529778154\/Facebook_normal.jpg', 
      text = 'Lo real es lo real by @AnaliaRob tratando de expresarse vaya 1 a saber sobre q! Es un proverbio milenario. Only xa genios' 
     ); 
    private $tweet2 = new Tweet(
      id: 253324444091703298, 
      created_at: 'Wed, 03 Oct 2012 02:43:48 +0000', 
      profile_image_url: 'http:\/\/a0.twimg.com\/profile_images\/1529778154\/Facebook_normal.jpg', 
      text: 'Lo real es lo real by @AnaliaRob, trantando de expresarse vaya uno a saber sobre que!' 
     ); 

    return $tweets; 
} 

class Tweet{ 
    private $id = null; 
    private $created_at = null; 
    private $profile_image_url = null; 
    private $text= null; 
}; 

非常感谢。

回答

1
$tweets = array($tweet1, $tweet2); 
return $tweets; 

编辑:

要解释,你可以不填充在声明区阵列($tweets),因为$tweet1$tweet2尚不存在,声明/在功能getTweets()它们分配。

如果您不需要在其他地方访问$tweets在你的代码,你可以跳过$tweets声明和return array($tweet1, $tweet2);getTweets()如图@MarvinLabs。

1
$tweets[] = $myTweet; 

$tweets = array($tweet1, $tweet2); 

编辑:

public function getTweets(){ 
    private $tweet1 = new Tweet(
      id = 253338336415064064, 
      created_at = 'Wed, 03 Oct 2012 03:39:00 +0000', 
      profile_image_url = 'http:\/\/a0.twimg.com\/profile_images\/1529778154\/Facebook_normal.jpg', 
      text = 'Lo real es lo real by @AnaliaRob tratando de expresarse vaya 1 a saber sobre q! Es un proverbio milenario. Only xa genios' 
     ); 
    private $tweet2 = new Tweet(
      id: 253324444091703298, 
      created_at: 'Wed, 03 Oct 2012 02:43:48 +0000', 
      profile_image_url: 'http:\/\/a0.twimg.com\/profile_images\/1529778154\/Facebook_normal.jpg', 
      text: 'Lo real es lo real by @AnaliaRob, trantando de expresarse vaya uno a saber sobre que!' 
     ); 
    // Option 1 
    return array($tweet1, $tweet2); 

    // Option 2 
    $result = array(); 
    $result[] = $tweet1; 
    $result[] = $tweet2; 
    return $result; 
} 
+0

我应该在哪里填充它,在声明或方法返回?非常感谢你! –

0

您可以访问它们这种方式,功能getTweets内:

$tweets = array($tweet1, $tweet2,...);