开发者

Do I need to grab a PHP class object in every method in a class?

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 objec开发者_如何学Got, 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


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.


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.


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();


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.


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜