Home arrow PHP arrow Page 3 - Building a Complete Web Searching Class with Yahoo Web Services and PHP 5

Using Inheritance to work with different Yahoo! Web Services - PHP

Welcome to the final part of the series “Using Yahoo! Web Services with PHP 5.” As the title claims, this series walks you through the basic concepts surrounding the correct utilization of the most relevant web search services provided by Yahoo! and puts the corresponding theory into practice by utilizing both procedural and object-oriented approaches.

TABLE OF CONTENTS:
  1. Building a Complete Web Searching Class with Yahoo Web Services and PHP 5
  2. Defining an image searching class in PHP 5
  3. Using Inheritance to work with different Yahoo! Web Services
  4. Consuming specific Yahoo! Web Search Services
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
February 20, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap

Dev Shed Tutorial Topics: