Reputation: 1780
I am starting to use namespaces, but I'm having a difficult time understanding when to use them.
Here's an example of the situation I have:
$date = ''; // variable to be used in all methods
function get_sports_data() {
get_sports_data_yahoo();
get_sports_data_google();
get_sports_data_bing();
}
function get_sports_data_yahoo() {} // uses $date
function get_sports_data_google() {} // uses $date
function get_sports_data_bing() {} // uses $date
I don't expect to use the helper functions individually.
get_sports_data
with methods for yahoo, google, and bing?I asked the PHP chat, but I think they were busy.
Upvotes: 2
Views: 229
Reputation: 3470
Without knowing anything about the rest of the application it is tough to say what you need, but my initial thought is that you may have three classes and an interface there...
It seems a bit like overkill, but since you're asking about OO design, that would be my natural starting point.
Namespaces are primarily used to group classes and interfaces together when they make up a consistent module or library. Generally it is done so people can use multiple libraries and not worry about names clashing.
I.e. each set of names has their own space, and they never run into each other.
It might be that you decide to give these a namespace but probably only if they provide a chunk of functionality you think you might want to use elsewhere or put out for public use.
Upvotes: 2