Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Class ArrayList not found..


Reply


2 Posts   561 Views

Avatar
borriej

Community Member, 267 Posts

4 September 2012 at 8:53pm

Ok simple question, why do i get this error:

Fatal error: Class 'ArrayList' not found in /public/sites/..../mysite/code/Tweet.php on line 68

my code

Tweet.php

<?php

/**
* Loads tweets onto a SilverStripe object through a XML file rather than the
* web API.
*
* Usage:
*
*    // PHP
*   function Tweets() {
*       return Tweet::get('http://twitter.com/statuses/user_timeline/willrossi.rss', 10);
*    }   
*
*   // SS
*   <% if $Tweets %>
*      <% loop $Tweets %>
*         $Description on $PubDate.Nice
*      <% end_loop %>
*   <% end_if %>
*
* @license http://sam.zoy.org/wtfpl/COPYING
* @author Will Rossiter (will@fullscreen.io)
*/

class Tweet extends ViewableData {

   protected $description;
   protected $pubDate;

   public function __construct($description, $pubDate) {
      $this->description = $description;
      $this->pubDate = $pubDate;
   }

   /**
    * Parses hash tags, @usernames, and URLs in a tweet description into
    * HTML links.
    *
    * @return string
    */
   public function getDescription() {
      $desc = $this->description;

      $desc = substr(strstr($desc, ': '), 2, strlen($desc)); // remove preceding username
      $desc = preg_replace("/(http:\/\/)(.*?)\/([\w\.\/\&\=\?\-\,\:\;\#\_\~\%\+]*)/", "<a href=\"\\0\">\\0</a>", $desc); // URLs into links
      $desc = preg_replace('/(^|\s)#(\w+)/', '\1<a href="http://search.twitter.com/search?q=%23\2">#\2</a>', $desc); // Hash tags to links

      return DBField::create_field('HTMLText', $desc);
   }

   /**
    * @return SS_Datetime
    */
   public function getPubDate() {
      return DBField::create_field('SSDatetime', $this->pubDate);
   }

   /**
    * Get some tweets from static file with a limit.
    *
    * @param int $limit Limit number of tweets
    * @return ArrayList
    */
   public static function get($path, $limit) {
      $cache = SS_Cache::factory('tweets');

      if (!($output = unserialize($cache->load('tweets')))) {
         $output = new ArrayList();
         $context = stream_context_create(array(
            'http' => array(
               'method' => 'GET',
               'timeout' => 5
            )
         ));

         $tweets = @file_get_contents($path, '', $context);
         if(!$tweets) return false;
         
         $xml = new SimpleXMLElement($tweets);
         $total = 0;

         foreach($xml->xpath('channel/item') as $item) {
            $output->push(new Tweet(
               (string) $item->description,
               (string) $item->pubDate
            ));

            $total++;

            if($total == $limit) break;
         }

         $cache->save(serialize($output));
      }

      return $output;
   }
}

and inside homepage.php

class Homepage_Controller extends Page_Controller {
      
   function Tweets() {
      return Tweet::get('http://twitter.com/statuses/user_timeline/myusername.rss', 5);
   }   

Avatar
Willr

Forum Moderator, 5513 Posts

7 September 2012 at 10:23pm

FYI that code was written for 3.0. You should have a framework/model/ArrayList.php file with the class in it.