Page 1 of 1

Objects interacting with other Objects

Posted: Wed Oct 27, 2004 2:37 pm
by The Monkey
Hello guys,

I have an interesting, although maybe not unusual, situation.

Being the organized guy I am, I really appreciate classes for their structure alone: I can look at the variable prepending a function and instantly tell the general idea of what that function is supposed to do. They are also nice for keeping unneeded functions and variables out of the way.

Anyway, with my current project, I have several classes, but the ones in question are:
  1. accounts.class
  2. sessions.class
class Accounts takes care of all functions related to my users', such as querying their profile information, updating their preferences', and validating input related to their account (such as usernames).

class Sessions is responsible for creating, continuing, and destroying sessions.

The problem lies in the Sessions class communicating with the Accounts class: For instance, if a user returns and has a "remember me" cookie, then the Sessions class would need to utilize the Accounts class to determine the information to load into the current Session.

So, what is the best way to load a classes variable ($class = new Class()) into another class? I have several functions, and global $class in every function seems a bit excessive. Is there any way to load a global variable for an entire class? Or should I find a different way to structure this setup?

- Monkey

Re: Objects interacting with other Objects

Posted: Wed Oct 27, 2004 2:58 pm
by timvw
The Monkey wrote:accounts.class takes care of all functions related to my users
For me that would be a reason to call the class User... As instances represent a User.

The Monkey wrote: The problem lies in the Sessions class communicating with the Accounts class: For instance, if a user returns and has a "remember me" cookie, then the Sessions class would need to utilize the Accounts class to determine the information to load into the current Session.
I would store the "User" object in the Session. So for each page you need to initialize a Session, which would have methods get|setUser. and the getUser function would return the unserialized "User" instance (if any) where the method setUser would serialize the user intance...


don't really see the problem?

Posted: Wed Oct 27, 2004 3:01 pm
by kettle_drum
You can either have them inherit from each other or simply pass the object to the class you are creating:

Code: Select all

class A {
   function rahh(){
      echo "hello";
   }
}

class B {
   var $classes;
   function B(&$class){
      $this->classes = $class;
   }
}

$foo = new A();
$bar = new B($foo);

$bar->classes->rahh();
That help?

Posted: Wed Oct 27, 2004 3:04 pm
by The Monkey
kettle_drum wrote:You can either have them inherit from each other or simply pass the object to the class you are creating:

{code}

That help?
Ah, that is exactly what I was looking for!

- Monkey, who will also consider what Tim said about his class's name, and should probably start asking simple questions as opposed to dictionary-length ones