nerdular Posted August 15, 2007 Share Posted August 15, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/64980-find-the-object-that-called-this-object/ Share on other sites More sharing options...
emehrkay Posted August 15, 2007 Share Posted August 15, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/64980-find-the-object-that-called-this-object/#findComment-324353 Share on other sites More sharing options...
keeB Posted August 15, 2007 Share Posted August 15, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/64980-find-the-object-that-called-this-object/#findComment-324383 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.