- Joined
- Jul 23, 2012
- Messages
- 201
- Reaction score
- 128
Hello in this tutorial i will show you some basic tips and tricks.
1. Default session variable assignment...
Most of you(including me at first ) sets default variables for session like this:
Problem: To much checks each time page is loaded, as these are needed to load only once, there is nothing wrong with this at all but we can make this better and more readable.
Solution: Lets add only one isset check for user first visit.
2. Class auto loading
Class auto loading could be expensive but there are always pros and cons. One of the huge impact could be that php would load only classes when you require them. And you do not need to include them manualy...
Class naming example using this code: for example you have class in folder: "sys/core/module.php" the class name will be: "sys_core_module". You should avoid over complicate auto loading functions... This auto loader will load public static function autoLoad() if it exist in class.
3. Class/function/language/config loading
Using loader code you do not need initialize new class or include config file over and over if config file or class is already loaded it's reference is returned for you..
4. Resolving full web location paths.
This following script will return full path to current web public_html folder including subfolders
this must be put in index.php file!
1. Default session variable assignment...
Most of you(including me at first ) sets default variables for session like this:
PHP:
if(!isset($_SESSION['token']))
{
$_SESSION['token'] = md5(uniqid(rand(), true));
}
if(!isset($_SESSION['language']))
{
$_SESSION['language'] = 'english';
}
if(!isset($_SESSION['access']))
{
$_SESSION['access'] = 'guests';
}
Solution: Lets add only one isset check for user first visit.
PHP:
if(!isset($_SESSION['active']))
{
$_SESSION['signed'] = false;
$_SESSION['language'] = 'english';
$_SESSION['template'] = false;
$_SESSION['token'] = md5(uniqid(time()));
$_SESSION['active'] = true;
}
2. Class auto loading
Class auto loading could be expensive but there are always pros and cons. One of the huge impact could be that php would load only classes when you require them. And you do not need to include them manualy...
PHP:
spl_autoload_register(function($class){
$name = str_replace('_',DIRECTORY_SEPARATOR,$class);
include $name.'.php';
if(method_exists($class,'autoLoad')){
$class::autoLoad();
}
});
3. Class/function/language/config loading
Using loader code you do not need initialize new class or include config file over and over if config file or class is already loaded it's reference is returned for you..
PHP:
class sys_core_loader {
/**
* @var array
*/
public static $extensions = array();
/**
* @var array
*/
public static $languages = array();
/**
* @var array
*/
public static $configurations = array();
/**
* @param $name
* @return mixed
*/
public static function load($name)
{
if(!isset(self::$extensions[$name])){
self::$extensions[$name] = new $name;
}
return self::$extensions[$name];
}
/**
* @param string $name
* @param bool $onFailDefault
* @return bool false if language file do not exists
* null null if language file is empty or incorrect format
* array array if language file was loaded
*
*/
public static function loadLanguage($name,$onFailDefault = true)
{
if(!isset(self::$languages[$name])){
if(is_file(X_PATH_APP.'language/'.$_SESSION['language'].'/'.$name.'.php')){
$language = NULL;
include X_PATH_APP.'language/'.$_SESSION['language'].'/'.$name.'.php';
self::$languages[$name] = $language;
}
else{
if($onFailDefault == true){
$config = self::loadConfiguration('common');
if(is_file(X_PATH_APP.'language/'.$config['language'].'/'.$name.'.php')){
$language = NULL;
include X_PATH_APP.'language/'.$config['language'].'/'.$name.'.php';
self::$languages[$name] = $language;
}
}
else{
return false;
}
}
}
return self::$languages[$name];
}
/**
* @param $name
* @return bool
*/
public static function loadConfiguration($name)
{
if(!isset(self::$configurations[$name])){
if(is_file(X_PATH_APP.'config/'.$name.'.php')){
$config = NULL;
include X_PATH_APP.'config/'.$name.'.php';
self::$configurations[$name] = $config;
}
else{
return false;
}
}
return self::$configurations[$name];
}
}
4. Resolving full web location paths.
This following script will return full path to current web public_html folder including subfolders
this must be put in index.php file!
PHP:
define('X_PATH_BASE', dirname(__FILE__));
define('X_PATH_SYSTEM', X_PATH_BASE.'/sys/');
define('X_PATH_ASSETS', X_PATH_BASE.'view/assets/');
define('X_PATH_APP', X_PATH_BASE.'app/');
include X_PATH_SYSTEM.'system.php';