Should my session class be static?

I've always been told that be very careful when creating static classes, as they tend to abuse less experienced programmers.

I am currently writing a session class in PHP to track a user on a site.

It seems that the session makes sense to be in a static class, because only one of them will be correct?

I worked in CMS with a factory that created a session object, but then saved the instance, and when a new session was requested (MyFactory :: getSession ()), it will return the previously initialized one. Is there any advantage to this?

Thank!

+5
source share
1 answer

, , - . . Static factory ( ) , SRP.

- , - , .

, . , factory.

class ObjectFactory
{

    protected $session;

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

    public function create( $name ) 
    {
        return new $name( $this->session );
    }

}

factory , , . factory, .

:

$session = new Session;

$user = new User( $session );
$article = new Document( $session );

$user $article .

:

+4

All Articles