0

Currently I have my controller instantiating a model class. Every time I create this class I need to set the logged in user's info in the model. I feel like there is a more elegant way of doing this, but I'm not sure how to do it :-(

Here is an example of the code:

$leadModel = new Application_Model_DbTable_Leads; $leadModel->user = $this->user; 

What I would like to do from inside the model I'm creating is access the user using something like this (I know this only applies to classes that are extending other classes):

$user_id = $this::parent->user; 

Thanks so much!

1
  • 1
    You may want to read up on dependancy injection. Passing around the items the model requires, so to speak. Commented Aug 27, 2010 at 17:34

1 Answer 1

2

This is usually done using a constructor parameter:

class Application_Model_DbTable_Leads { public function __construct($user) { $this->user = $user; } } ... $a = new Application_Model_DbTable_Leads($user); 

There is no way to get a reference to the instantiating class.

Sign up to request clarification or add additional context in comments.

1 Comment

It's possible to lookup an object reference with the us2.php.net/debug_backtrace function, but you definitely shouldn't be doing it that way

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.