HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

PHP framework building: Sessions Managment Class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
classphpbuildingmanagmentframeworksessions

Problem

I am building a PHP framework and would like to get some feedback on a few different sections of the project so far. I consider myself still a neophyte in PHP so I would like to ask if I'm going about completing these different tasks in an efficient and or correct way.

I am currently working on a session management class. What I have done is create methods that deal with a specific action when dealing with sessions. Pinoniq has suggested to me:


Always think this: everything you code must solve some kind of
problem. If it doesn't solve a problem. Remove it.

So I kept that in mind when creating this class.

What I am trying to do in this class is create methods that do most of the work for you when creating and working with sessions.

Methods like namePattern() and ParamBreak() are operations for use inside the class class method.

```
class Sessions {
/**
* Start a session
*
* @param $id string - Set sestion id
* @return boolean or trigger_error
*/
public function StartSession($id = null){
if(!session_start()){
trigger_error('Unble to create session',E_USER_WARNING);
}
if($id != null && is_string($id)){
//Check pattern to make sure it's supported
if($this->namePattern($id)){
return session_id($id);
}
}
return true;
}
/**
* Create a session value in the $_SESSION array
* if key is not supplied then one will be
* created and incremented
* automaticly
*
* @param $key string - Index key for array
* @param $value mixed - value to be stored
*/
public function CreateSession($key = null,$value = null){
if(!$key && !$value){
trigger_error('Missing argument for Sessions::CreateSession()');
}
if(empty($value)){
$value = $key;
$key = null;
}
$_SESSION[$this->FindSessionKey($key)] = $value;
return true;
}
/**
* Return or set the session_id
*
* @param $id string - string to set the id to
* @return current session_id
*/
public func

Solution

First off the code is hard to make sense of, because I only see part of it. Most importantly I don't have the source for SessionsStruction class, which you are extending, and the name "Struction" doesn't make any sense to me (is it even in dictionary?).

It would also help to have a code sample of using this class.

The class also has no documentation describing its purpose - and after reading it all, I'm still wondering what's this all about.

Some immediate problems I see with this class:

-
It has too many responsibilities - parsing config file and debugging the sessions data (in ShowSessions method) should be handled outside of this class.

-
It has too wide API - an entire 13 public methods. Do you really need to provide such fine-grained interface?

-
It depends on global state: APP_PATH, CURRENT_FILE are defined somewhere else. Consider passing them in as parameters instead. Also the $_SESSION var.

-
why the empty __destroy() and SearchSessions() methods?

-
why the RemoveSession() which just delegates to RemoveSessionByKey()?

-
why the unused $active and $maltivalue class variables?

-
you really should check your spelling.
-

Context

StackExchange Code Review Q#43620, answer score: 5

Revisions (0)

No revisions yet.