tags:

views:

80

answers:

5

I am working on some PHP classes, I have a session class used to set and get values to session variables, I will need to have access to this session class object in every other class so I made a singleton method in the session class and then in other class's methods I can call the session object like this....

$session = Session::getInstance();

This will return the session object to me to use or else start a new session object if one has not been started yet.

So my question, if I have a user class and a database class and each class had 10 methods in each that needs to access the session object, then would I need to run the code above inside each and every method or just 1 time inside the class and then all methods would have it? I am new to this so I am not sure??? Thanks

+2  A: 

In a Singleton pattern, you would indeed have to do that.

An alternative might be to add the session object as a protected member $session of your class in the constructor, and then accessing it from each method as $this->session.

If you want to read some broader discussion about how to arrange and use helper and library objects in PHP, I asked a related SO question once that yielded interesting answers.

Pekka
Thanks that might be a good solution instead of repeating the same code over and over, woulld I then access the function or variable of the session object like this, $this->session->get() or like this $session->get() or some other way? thanks
jasondavis
`$this->session->get()` it is. @Pikrass has a complete example in his answer.
Pekka
very cool, thank you
jasondavis
+1  A: 

Run your method in the constructor of each class and store the resulting object as an instance variable.

class Database {
    private $session;

    function Database() {
        $this->session = Session::getInstance();
    }
}

Then you can access to your instance of Session in the methods with $this->session.

Pikrass
thanks good example
jasondavis
A: 

Alternatively, you could make this $session a global variable instead.

$GLOBALS["session"] = Session::getInstance();

You can then manipulate $GLOBALS["session"] from anywhere.

But if you want your code to measure up to the idealized discipline of OOP, stick with Pekka's answer :)

LeguRi
+1  A: 

You'll want set your session in your constructor. For more flexibility though, it may be better to pass the session object in:

class User {

  private $session;

  public function __construct(Session $session) {
    $this->session = $session;
  }

  public function methodOne() {
    $stuff = $this->session->getStuff();
    // etc...
  }

}

// example usage
$session = Session::getInstance();
$user = new User($session);
$user->methodOne();

This method helps ensure that your User object is not tied to that specific Session implementation. As long as further Session objects have the same interface, your User object can happily use any without issue.

// example usage
$session = SuperSession::getInstance(); // SuperSession is like Session,
                                        // but better! same interface though.
$user = new User($session);
$user->methodOne();
Owen
+1  A: 

Because PHP handles objects by reference, technically you could have a base class that captures an instance of the session in the constructor

abstract class AbstractSessionEnabledClass {

    public function  __constructor() {
        $this->session = Session::getInstance();
    }

    private $session = null;
}

And then have your sub classes extend this class

class SomeClass extends AbstractSessionEnabledClass {
    public function __constructor() {
        parent::__constrcutor(); //this isn't needed if this class has no constructor
    }

    public function someMethod() {
        var_dump($this->session)
    }
}

This kind of encapsulation will actually make your code easier to modify in the event that session handling ever changes.

AndrewMurphy