Objects interacting with other Objects

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Objects interacting with other Objects

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Objects interacting with other Objects

Post 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?
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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?
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post 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
Post Reply