0

I'm building a class that interacts with an API. Say the API has a "get_something" method for "foo" objects and "bar" objects. I want my class to expose a "get_something" method, but be able to distinguish if it's for "foo" or for "bar."

What's a good solution for me? Can I create a class that has multiple name spaces? Would that be a good idea?

Maybe I should have nested classes?

2 Answers 2

1

you should look at namespaces as packages in java. a class belongs to 1 package only

what you can do though is have

namespace A_NS; class A { ...} namespace B_NS; class A extends \A_NS\A {}; 

in that case class A will exist under namespace A_NS, and another class A' will exist under B_NS, which will extend class \NS_A\A;

you could check an object's class, or implement some identifier inside to distinguish.

overall, i would recommend that the design of your system will treat each class as if it belongs to 1 namespace only.

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

3 Comments

I want one class to interact with the API. Should I just use some strategic naming convention? For example foo_get_something, bar_get_something, etc.?
what's the API for? if you have an API for something, you should (in theory) have a library that interacts with it alone. it is supposed to be some abstraction. your system interacts with the library, and is unaware of the API interaction code. your library can consist of one class or several classes (in the same namespace). the idea is that the library can be used in different project, is and independent of the rest of the system. that's the overall recommended approach
in your case btw, you can do get_something('foo', ...) - ie add an identifier for type. the more OO approach would say different methods for "foo" and "bar" objects
0

Two ways to do this, you can use an interface (http://us3.php.net/interface), which ensures that classes implementing the interface will have specified methods. Or create a parent class with abstract method (http://us3.php.net/abstract) get_something which will force any child classes to also have the class. Then you should be able to do a get_class($object) to determine which class was instantiated.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.