0

Hi I'm new to PHP I would like to know hoy to populate the tweets array in order to return it.

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

thank you very much.

3 Answers 3

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

EDIT :

To explain, you can't populate the array($tweets) in the declaration zone because $tweet1 and $tweet2 doesn't exist yet, you declare/assign them in the function getTweets().

If you don't need to access $tweets elsewhere in your code you can skip the $tweets declaration and return array( $tweet1, $tweet2 ); in getTweets() as shown by @MarvinLabs.

Sign up to request clarification or add additional context in comments.

Comments

1
$tweets[] = $myTweet;

or

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

Edit:

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

1 Comment

Where should I populate it, in the declaration or in the method return? Thank you ery much!
0

You can access them this way, inside the function getTweets:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.