Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Since the methods of the class will only work if the user is logged in, is it right or is there some problem that might make my code slow/inneficient?

share|improve this question

I suggest you handle login separately (outside of your API class) and use PHP's session module to confirm the current session is authenticated.

share|improve this answer
-1 You don't know what language the OP is using. – Michael K Jan 10 '12 at 18:18
According to the OP's post tag, it's PHP. – sarumont Jan 10 '12 at 18:20
Hmm. Sorry, missed that. I'd cancel the downvote but it's locked in now. – Michael K Jan 10 '12 at 18:44
@Michael: If the answer is edited you can change your vote. – Ethan Furman Jan 10 '12 at 20:18
Edited to allow that. :) – sarumont Jan 10 '12 at 21:02

There's certainly nothing wrong with forcing your users to pass in credentials when they instantiate a copy of your service client, if that's what you want them to do.

However, you have to be careful. You can NEVER trust ANYTHING your consumers give you. So, say this is a web service, for which you are providing a service client API that just plugs in. You cannot be sure that the code that is calling your service is your service client, being used as you intended. Pretty much any communications protocol can be reverse-engineered as to its methodology and form, if not the exact data being sent in any given usage.

Therefore, if it is important for the user to be authenticated before doing anything else, I would check for authentication as the first step of any other task the service can perform. That does not preclude the development of a "login constructor", but it's an extra level of code security that protects you if a consumer should try to get around your implementation.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.