I had originally posed this on StackOverflow. The question was closed and I was asked to post this here instead. Here is the original question I had posted there:
I have been programming using procedural method for a while now. I have done some good amount of reading pertaining to OOP and I know how to use it. It's just that I am not sure if whether I still know how to use it correctly? So to put this to a test I am presenting a skeleton of a plugin which I have developed using OOP. The plugin is supposed to fetch values like subscribers, follows etc from various social networks.
// This is an abstract class used by all Social Network classes
abstract class Social_Network {
protected $service_url,$service_identifier,$icon_url,$refresh_data;
function __construct($arguments) {
// Code to populate the protected variables declared above
}
protected function get_value_internal() {
// Internal here means get data from cache.
// Call get_value_external If cache data has expired or does not exist
}
protected function parse_json_data($data) {
// This function parses data and gets the requested value from it
}
public function display_value() {
// This is a public function which outputs the value
// Call get_value_internal
// Display the value with icon, etc.
}
//This will be different for all networks because every network would have a different method for retrieving data
abstract function get_value_external();
}
class Facebook_Social_Network extends Social_Network {
function get_value_external() {
// Unique code for retrieving data from Facebook
// Process the data fetched above and parse it using the parse_json_data function defined in our abstract class
return $this - > parse_json_data($data);
}
}
class Twitter_Social_Network extends Social_Network {
function get_value_external() {
// Unique code for retrieving data from Twitter
// Process the data fetched above and parse it using the parse_json_data function defined in our abstract class
return $this - > parse_json_data($data);
}
}
class YouTube_Social_Network extends Social_Network {
function get_value_external() {
// Unique code for retrieving data from YouTube
// Data returned by YouTube is processed differently so this doesn't use the shared method parse_json_data
// Process the data and put it inside a variable named $value which will be returned below
return $value;
}
}
I will appreciate if someone good at OOP can take a quick look and tell me if I am on target with my understanding, close to the target or way off?
One thing that's bothering me is whether I should have just used one class which would handle all Networks?