Since the web service classes that you learned before are very similar to each other, it's preferable to take a more efficient approach and build a base class that encapsulates as much common functionality as possible. Next, derive some subclasses that implement (or eventually) override some of the parent's method in order to use a specific Yahoo! Web Service. Of course, to do so I have to appeal to Inheritance, but first let me show you the signature of this base web searching class. It is as follows: // define abstract 'SearchService' class abstract class SearchService{ protected $requestPath; protected $query; protected $numResults; protected $output; public function __construct(){ $this->query='madonna'; $this->numResults=10; $this->output=''; } // 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; } // define a generic method to perform web search and get search results public function getSearchResults(){} } As you can see, in this case I simply defined the previous "SearchService" class abstract, since I don't want it to be instantiated later on. However, this class also defines one generic "getSearchResults()" method (aside from the corresponding constructor, of course), which needs to be implemented in a concrete way by all the subclasses derived from this parent to use a specific Yahoo! Web Search Service. Defining the previous classes this way permits us to build some child classes, which will be responsible for consuming a concrete Yahoo! Web service. Undoubtedly, this is a clear example of how useful Inheritance can be in a real-world situation. All in all, at this point I'm pretty sure that you understand how the prior "SearchService" class was built, so it's an appropriate moment to learn how the respective subclasses will be derived from this parent. Indeed, this process will be quite interesting, so to learn more, please click on the link below and read the following section.
blog comments powered by Disqus |
|
|
|
|
|
|
|