After a long long time I was back to coding in php. And by long time, I mean atleast 24 months ! Believe me, it wasn’t easy. More so because of the higher standard I have set for myself. My code this time shall be much neater and easier to read than it ever was.
The past four years of my life has been spent coding. And when you code so long, you modify your habits to get tuned to the proper way of coding. As it is said, a person spends more time reading a code than writing it. So this time I am taking special care of writing simpler and smaller functions so as to make the code more readable. Also, since I have been reading a lot of OOP these days, I am intent on providing an abstraction layer for the user, so that at the top most level coding should appear like writing a few english sentences rather than some cryptic mysqli_select_db things.
This is the first time I am trying to code in php following the OOP structure. Also, though this might seem strange, it is the first time I am using session variables in my design! Below is a trivia I had not known earlier, and spent 2 hours trying to understand what was wrong in my seemingly proper and correct code.
A session created with session_start will only be available to pages within the directory tree of the page that first created it.
i.e. If the page that first creates the session is /dir1/dir2/index.php and the user then goes to any page above dir2 (e.g. /dir1/index.php), session_start will create a new session rather than use the existing one.
Also I found the following class to be useful.
< ?php
class Session {
public static function Init() {
self::Commence();
}
public static function Set($fld, $val) {
self::Commence();
$_SESSION[$fld] = $val;
}
public static function un_set($fld) {
self::Commence();
unset($_SESSION[$fld]);
}
public static function Destroy() {
self::Commence();
unset($_SESSION);
session_destroy();
}
public static function Get($fld) {
self::Commence();
return $_SESSION[$fld];
}
public static function is_set($fld) {
self::Commence();
return isset($_SESSION[$fld]);
}
private function Commence() {
if(!isset($_SESSION['ready'])) {
session_start();
$_SESSION['ready'] = TRUE;
}
}
}
?>
Popularity: 2% [?]
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=58b9f408-d614-440c-85a3-11462c55f08b)
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=c8d10e09-3cac-470a-b3ad-3d3a8091bac2)