954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Passing Variables between objects

Hi all, been dipping my toe into OOP and am getting on OK, but I'm hitting a bit of a wall with a current project.

classes:
db (a PDO extended class)
User
Timetable

The db is just a few shorthand ways of running PDO
The User is just for handling the current user's details, e.g. profile etc.
The Timetable is for showing the user's current timetable (and a few other things)

I've currently got this thing going on in my page:

include("includes/config.php");
include("class/class.DB.php");
include("class/class.User.php");
include("class/class.Timetable.php");

$db = new db($config['db']);
$u = new User($db);
$t = new Timetable($db,$u);


where I pass the db object to both user and timetable and pass the user object to the timetable. Is this the best way to do things?


In my timetable constructor I have this:

$this->db = (object) $db;
$this->user = (object) $u;

But if I want to use the user's 'detail' property in the timetable class, I have to make $detail public - protected won't work. Does protected only work for child classes?If so, must I make these public? Don't like the idea of that.

in user class:

public $detail;


to use it thus, in timetable (example):

$where = ($this->user->detail['rights'] > 7) ? "" : " WHERE s.staff_id = {$this->user->detail['staff_id']}";


I'm still a little off with abstract classes and interfaces, so apologies if I need to do something with those - I'm still learning.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

The way you describe it, it sounds like the timetable should be a part of the user.

And yes, protected can be accessed only by ancestors and descendants.

pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
The way you describe it, it sounds like the timetable should be a part of the user.

And yes, protected can be accessed only by ancestors and descendants.


Ok - protected now makes sense.
When you say timetable should be part of user, do you mean as a property?
Thinking about it, that does sound sensible.
Thanks pritaeas.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: