0

I place the following at the top of every page on my site.

require_once "classes/Session.php";

This class:

require_once 'Form.php';
require_once 'Database.php';

class Session{

public $referrer;
public $logged_in;
public $url;

public function __construct(){

    $this->startSession();

}

public function startSession(){

    session_start();

    $this->logged_in = $this->checkLogin();

    //set last page visited
    if(isset($_SESSION['url'])){
        $this->referrer = $_SESSION['url'];
    }
    else{
        $this->referrer = "/";
    }

    //sets the current URL
    $this->url = $_SESSION['url'] = $_SERVER['REQUEST_URI'];
}

}

I've changed my approach to how I handle this.

I used to put new Session at the bottom of Session.php but that isn't pure OOP?

What I don't understand is where to create the Session object so I can use it on my page?

I'm a bit confused about this but feel I'm missing something basic to just get me thinking on the right lines.

Any help would be great!

Don
  • 857
  • 1
  • 7
  • 22
sark9012
  • 4,905
  • 16
  • 52
  • 90
  • 1
    You've encapsulated logic but it's not really an object.. atleast it doesn't take advantage of any OOP concepts. Look at http://stackoverflow.com/a/13293118/46675 – Mike B Dec 11 '13 at 19:56
  • By placing new Session in the file below the class, because this is loaded on every page reset, will that create an instance of the object each time? – sark9012 Dec 11 '13 at 19:57
  • When you include a php file/class, the scope is set to wherever the include is being done. In other words: the Session object you instantiate inside of the file will only exist on that page (if you call the include inside of a function, the Session will be scoped to that function). Once you leave the page, the Session object is gone. A singleton basically provides an access point to the single instance of Session - it can be called from other files/classes. Class with all static members works as well, but you lose the ability to treat Session as an object. – TonyArra Dec 11 '13 at 20:41

1 Answers1

1

It sounds like you're trying to make a Singleton class (a class that is only instantiated once). It's true that this is often considered non-OOP or bad-form, but common when dealing with things like database handlers. This might be helpful: Creating the Singleton design pattern in PHP5

Edit: If you don't want to use a Singleton, then you don't really need to use an object at all. Just make all the member functions and variables of Session static so that they can be called like Session::startSession(). You don't even need a constructor if you do this.

It's important to recognize when you actually NEED to use an object, and when you don't. An object is typically used when you want to have multiple instances - each with their own values. Objects can also be passed to other functions. If you don't need multiple instances or the ability to pass it as a function parameter, you just use a Static Class to encapsulate the logic. The Singleton design pattern is for cases where you still want to pass the object to methods and use it like an object, but you only want there to be one instance. You technically don't need a Singleton to do this.

Edit again again: Dependency injection is technically the best way to handle this stuff. You could pass around the Server object (or a container holding the Server object) to all your other constructors. That's the "pure" OOP method.

Community
  • 1
  • 1
TonyArra
  • 7,816
  • 1
  • 26
  • 33