Let’s move back through our code and replace all of the error comments with throw statements. private function fetchUserInfo($name) { $url = "http://twitter.com/statuses/user_timeline/{$name}.xml?count=1"; $xml = $this->curlRequest($url); if ($xml === false) { // User feed unavailable. } $statuses = new SimpleXMLElement($xml); if (!$statuses || !$statuses->status) { // Invalid user channel. } foreach ($statuses->status as $status) { $this->status_text = (string) $status->text; $this->profile_image = (string) $status->user->profile_image_url; $this->screen_name = (string) $status->user->screen_name; break; } } We first encounter error states in the fetchUserInfo method. It has two different error states. One occurs when the user feed is unavailable, and the other occurs when the script encounters an invalid user channel. private function fetchUserInfo($name) { $url = "http://twitter.com/statuses/user_timeline/{$name}.xml?count=1"; $xml = $this->curlRequest($url); if ($xml === false) { throw new Exception('User feed unavailable.'); } $statuses = new SimpleXMLElement($xml); if (!$statuses || !$statuses->status) { throw new Exception('Invalid user channel.'); } foreach ($statuses->status as $status) { $this->status_text = (string) $status->text; $this->profile_image = (string) $status->user->profile_image_url; $this->screen_name = (string) $status->user->screen_name; break; } } The conversion process is very easy. Simply replace all of the comments with throw statements. The comment itself works very nicely as the error message string for the Exception class. For the sake of space I won’t show you every instance, but you can follow this simple example to complete each of the remaining replacements.
blog comments powered by Disqus |
|
|
|
|
|
|
|