2

I am starting to use namespaces, but I'm having a difficult time understanding when to use them.

  • I want to group "helper methods"
  • I don't expect to extend this group (not really a class)
  • I think I want to change a variable instance

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.

  1. Would it best to use a namespace called get_sports_data with methods for yahoo, google, and bing?
  2. Would it be correct to set the date as an instance of an object or would it be better to pass the variable through each method?

I asked the PHP chat, but I think they were busy.

6
  • How about get_sports_data($filter=null), and $filter="yahoo"/etc ? Commented Nov 20, 2013 at 20:49
  • So combining all the functions into one big function? The functions are fairly large, it seems cleaner to divide them up. Would it be a good choice if they are large? Commented Nov 20, 2013 at 20:52
  • Based on the function names, I was guessing they do pretty much the same thing, with some different parameters based on the site. If I'm wrong, then no, keep them separate. Commented Nov 20, 2013 at 20:54
  • @Izkata They do very, very different things. I guess my example did not adequately describe this. Commented Nov 20, 2013 at 20:54
  • Small note in addition to the answer below: Had I seen something named more like "DataProvider" (for a class) or "retrieve_" (for a function) rather than "get_", it would have probably clicked right away that those three are going to be rather different Commented Nov 20, 2013 at 21:05

1 Answer 1

2

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...

  • Interface DataProvider defines the input and output of getSportsData
  • Classes then implement that interface:
    • YahooDataProvider
    • BingDataProvider
    • GoogleDataProvider

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.

Sign up to request clarification or add additional context in comments.

6 Comments

This is the application. It gets the data and prints it.
Obviously there's a lot more code that just this little snippet, and I'm really only trying to say that without more context it isn't possible to provide much design help. That said, I'd hope that a few pointers about good usage are all you're really looking for, isn't it?
I've looked into interfaces and it seems to fit my purpose, although using a class instead of a function does really seem like an overkill. Is there a "smaller" way of doing this?
Yep. Leave them as functions, as you describe. In the main classes, interfaces and the like don't really cost anything, so if you think they make your code more readable, more flexible or just plain 'nicer' then go for it. It's easier to try these things out in small systems and find out how to use them, than it is to wait until those systems get huge...
Thanks for your help, I will continue to read about interfaces. At least I know that namespaces would not be the correct use here.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.