I recently started to dive into Php classes and the first thing ( I hope not bad ) I did was to build all sorts of helper classes, mostly static, for the basic actions I do in every small-mid project. For example, I have a Session
class which has simple set/get/delete/flash methods so I can write easily Session::set('logged', 1)
. So I have all this small classes which I load with spl_autoload_register
.
This is my second most important question: Is it bad practice to build classes for trivial actions such as managing sessions, cookies, inputs, validations ?
Just for reference, this is what my session class looks like
class Session {
/*
check if $_SESSION is already started
*/
public static function isStarted() {
if ( php_sapi_name() !== 'cli' ) {
if ( version_compare(phpversion(), '5.4.0', '>=') ) {
return session_status() === PHP_SESSION_ACTIVE ? true : false;
} else {
return session_id() === '' ? false : true;
}
}
return false;
}
/*
check if a certain $_SESSION variable exists, e.g.: does $_SESSION['var'] exists ?
*/
public static function exists($name) {
if ( self::isStarted() ) {
return isset($_SESSION[$name]);
}
}
/*
check if the $_SESSION global variable is empty, e.g.: is it an empty array ?
*/
public static function isEmpty() {
if ( self::isStarted() ) {
return empty($_SESSION) ? true : false;
}
}
/*
call the session_start() function if it hasn't been already called
*/
public static function start() {
if ( !self::isStarted()) {
session_start();
}
}
/*
regenerate session id
*/
public static function regen() {
if ( self::isStarted() ) {
session_regenerate_id();
}
}
/*
set a new $_SESSION variable, e.g.: $_SESSION['var'] = 'value';
*/
public static function set($name, $value) {
if ( self::isStarted() ) {
$_SESSION[$name] = $value;
}
}
/*
get the value for a certain $_SESSION variable, e.g.: return $_SESSION['var'];
usage: Session::get('person.age'), Session::get('id'), Session::get('cart.id.qta')
*/
public static function get($name) {
$pieces = explode('.', $name);
if ( count($pieces) <= 1 ) {
return ( isset($_SESSION[$name]) ) ? $_SESSION[$name] : false;
}
$currentValue = false;
for ($i = 0; $i < count($pieces); $i++) {
if( !$currentValue ) {
if ( !isset($_SESSION[$pieces[$i]]) ) return false;
$currentValue = $_SESSION[$pieces[$i]];
} else {
if ( !isset( $currentValue[$pieces[$i]] ) ) return false;
$currentValue = $currentValue[$pieces[$i]];
}
}
return $currentValue;
}
/*
delete a certain $_SESSION variable, e.g.: delete and unset $_SESSION['var'];
*/
public static function delete($name) {
if ( self::exists($name) ) {
unset($_SESSION[$name]);
}
}
/*
delete all $_SESSION variables, e.g.: empty the $_SESSION global variable so it becomes an empty array
*/
public static function deleteAll() {
if ( self::isStarted() ) {
session_unset();
}
}
/*
display the $_SESSION global variable in a formatted way
*/
public static function show() {
if ( !self::isStarted() ) {
return dd($_SESSION);
}
}
/*
display a message one time, after refresh the message will be deleted
*/
public static function flash($name, $message = '') {
if ( self::exists($name) ) {
$message = self::get($name);
self::delete($name);
return $message;
} else {
self::set($name, $message);
}
return false;
}
/*
end and destroy the $_SESSION global variable
*/
public static function end() {
if ( self::isStarted() ) {
$_SESSION = array();
session_destroy();
session_unset();
setcookie(session_name(), '', time()-3600,'/', '', 0, 0);
}
}
}
The main question is about a wrapper class for PDO
. At the beginning I wanted to build a singleton
class, but as I am very easily influenced by others, I dropped the idea and I started to build a classic one with instantiation. Soon I discovered that for complex queries I would not gain any advantage by building this class. And to confirm my thoughts, after a little research I saw that 'experts' recomended not to build upon PDO
which has already a friendly usage.
So, do you think I can live with writing PDO
as it is, are there any real advantages with building a class for it ? I don't really mind writing all the code for queries, my main concern is the error handling and all the try
catch
blocks.
Thank you.
EDIT
I apologize for asking two question, I didn't want to open another post. My only question is about the PDO
class. Please ignore the first one.
Please note, I perform basic actions with the database, CRUD actions mostly and I won't be using multiple connections at once, at least for the immediate future.
About the try
catch
, it was referred to an eventual PDO
class because for me the error handling is the most difficult part so I thought that a class would handle it better than having to wrap all statement in try
catch
blocks.
As a matter of fact, you don't need try catch blocks for the error handling either. Or, rather, you shouldn't use it this way. PHP is smart enough to handle your errors for you better way.
So, the last argument is broken too.
As a matter of fact, PDO is almost ideal wrapper for basic queries.
To create a sensible helper class out of PDO is an extremely non-trivial task. So, it's better to stick with raw PDO, at least for starter.
There's really no need to build upon PDO like you would with the mysql or mysqli wrappers. PDO has pretty good built-in error handling and you can find where the hiccups are by the messages it provides when it encounters a problem. Now, if you've been using one of the mysql wrappers and are used to the custom error handling messages you've been looking at, then you might consider wrapping PDO in a class.
As far as class wrappers for SESSION and COOKIE handling, I don't see any problems with adding your own classes around them if that is what you prefer. After all, most of the major MVC frameworks provide their own way of handling SESSIONS and COOKIES.