Jump to content

Find the object that called this object?


nerdular

Recommended Posts

Forgive me for the rough craziness here but if I have an object that calls another object into existence, is there a way for the second object to which object called it? For example, I'm building an authentication class that will be used by many classes and have permissions based on what class it's being called by. So if the $agent class creates $agent->auth = new Auth, is there a way for $agent->auth to know that it's being called by 'agent'?

 

Any answers on this would be much appreciated ;)

Link to comment
Share on other sites

well what exactly do you want the auth class to do if it is being called by a certain class?

 

an idea would be to make the auth class a factory that would instinate the necessary child clases. ex

 

class auth{

__construct($class){

switch($class){

case 'agent':

return new authAgent();

break;

....//do this for the rest of the agent classes

}

//add your base-level auth methods

}

}

 

class agentAuth extends auth{

//agentAuth specific methods

}

 

using somethng like this, you only have access to the necessary methods in agentAuth and the base methods in auth

Link to comment
Share on other sites

The pattern describe above is known as the Abstract Factory

 

http://en.wikipedia.org/wiki/Abstract_factory_pattern

 

To better assess what you're trying to do, though, I'd like more detail on your design.

 

What problem are you trying to solve, and, are you willing to change your design?

 

Basically... your breaking the rule of encapsulation. Object A should not care about Object B, and if Object B depends so much on Object A, if you ever change Object A it causes a lot of problems in terms of maintenance. This is called a 'fragile base class' and should be avoided at all costs.

 

So, please explain what you're trying to do a bit more thoroughly :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.