I need to show you something before I teach you how to use Inheritance to build some child classes that will concretely implement each of the Yahoo! Web Search Services discussed in the previous articles of the series. Below you will see the corresponding definition of a simple class that is tasked with consuming the Yahoo Image Search Service, which was left undefined in the last tutorial. Here's the signature of this brand new PHP class, which is responsible for performing image searches using the corresponding Yahoo API. Take a look at it, please: // example using Yahoo! Image Search Service - results are displayed in a basic (X)HTML format utilizing a class // define 'ImageSearchService' class class ImageSearchService{ private $requestPath='http://search.yahooapis.com/ private $query='madonna'; private $numResults=10; private $output=''; public function __construct(){} // set search query public function setQuery($query){ if(!$query){ throw new Exception('Invalid search query!'); } $this->query=urlencode($query); } // set number of search results public function setNumResults($numResults){ if(!$numResults||!is_int($numResults)||$numResults>50){ throw new Exception('Invalid number of results!'); } $this->numResults=$numResults; } // perform web search and get search results public function getSearchResults(){ if(!$results=file_get_contents($this->requestPath.'&query=' throw new Exception('Error requesting Yahoo! Web service'); } $results=unserialize($results); foreach($results[ResultSet][Result] as $result){ $this->output.='<h2>'.$result[Title].'</h2><p>'.$result } return $this->output; } } As you can see, the definition of the above "ImageSearchService" class looks very similar to the ones that I defined in the previous tutorial, which were aimed at working with the respective Yahoo traditional and image web searches. In this case, I built this class so it can perform image searches very easily, which is certainly a process that's clearly demonstrated by the following hands-on example: try{ // create new instance of 'ImageSearchService' class $is=new ImageSearchService(); // set search query $is->setQuery('Dido'); // set number of search results $is->setNumResults(10); // display search results echo $is->getSearchResults(); } catch(Exception $e){ echo $e->getMessage(); exit(); } // displays the following dido_270.jpe http://fanwebdido.wz.cz/fotogalerie/dido_270.jpe Dido-Life_For_Rent-Frontal.jpg Enrique Iglesias - 7 50 cent-Get rich or die.. Dido - Life for rent http://www.coveralia.com/audio/d/Dido-Life_For_Rent- Dido_kontje.jpg http://xs45.xs.to/pics/05365/Dido_kontje.jpg dido_1.jpg http://peoples.ru/art/music/pop/dido/dido_1.jpg Dido.jpg http://biografie.leonardo.it/img/bio/d/Dido.jpg dido.jpg Pop chanteuse Dido entered London's Guildhall School of http://www.grabow.biz/images/dido.jpg dido.jpg dezembro 25, 2005 Parabans 1954 - Annie Lennox , cantora http://atuleirus.weblog.com.pt/arquivo/dido.jpg dido128.jpg Dido: 'I camped out the night before Wham! The Final'. http://image.guardian.co.uk/sys-images/Guardian/Pix/arts/ Dido2654.jpg http://www.sirensofsong.com/Dido/images/Dido2654.jpg INT-Dido.jpg Dido Born Florian Cloud de Bounevialle Armstrong, on the http://www.annecarlini.com/images/exclusive/interviews/INT-Dido.jpg Undoubtedly, consuming the Yahoo! Image Search Service by using the class that I built earlier is not only a no-brainer process that can be tackled with minor effort, but a fun experience! Okay, I have to admit that it actually isn't so fun for some, but keep in mind that I have to market this class to you somehow. Well, I assume at this time that the previous example should give you a clear idea of how to build a PHP class that utilizes the Yahoo! Image Search Service in a useful fashion. So, what's the next step? In accordance with the concepts that I deployed in the beginning, in the next section I'm going to teach you how to use Inheritance to create different subclasses. Each of them will be focused on consuming a specific Yahoo! Web service. In this manner, we will develop a more efficient object-oriented approach that will work with the services in question. To find out how these subclasses will be built, please jump ahead and read the next few lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|