0

嗨,我是 PHP 新手,我想知道如何填充 tweets 数组以返回它。

<?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;
};

非常感谢您。

4

3 回答 3

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;
}
于 2012-10-05T14:02:07.620 回答
1
$tweets = array( $tweet1, $tweet2 );
return $tweets;

编辑 :

解释一下,你不能$tweets在声明区填充 array(),因为$tweet1$tweet2还不存在,你在函数中声明/分配它们getTweets()

如果您不需要访问$tweets代码中的其他地方,您可以跳过$tweets声明并return array( $tweet1, $tweet2 );输入,getTweets()如@MarvinLabs 所示。

于 2012-10-05T14:13:18.720 回答
0

您可以在函数内部以这种方式访问​​它们getTweets

  $tweets = array( $tweet1, $tweet2,...);
于 2012-10-05T14:11:36.123 回答