1
If you are using xoopsObject to manage the data access to your modules tables you must surely use this kind of code:
$myHandler = xoops_getmodulehandler('mytable', 'mymodule');
The problem is that sometimes you declare this kind of code somewhere and you can't access it somewhere else, for example from a function.
The other problem is that you have to repeat this kind of code each time you need a handler.
With this class, you can access to any table's handler from anywhere without multiplying the objects.
You just need to do something like this :
$oledrion_handlers = oledrion_handler::getInstance();
$count = $oledrion_handlers->h_oledrion_products->getRecommendedCount();
You can even access a handler like this:
$count = oledrion_handler::getInstance()->h_oledrion_products->getRecommendedCount();
The advantage, as I said, is that you can access your handlers from anywhere without creating several times the same object and all your handlers are centralized in one place.
Handlers are loaded on demand and stay in memory for further use.
/**
* ****************************************************************************
* oledrion - MODULE FOR XOOPS
* Copyright (c) Hervé Thouzard of Instant Zero (http://www.instant-zero.com)
*
* You may not change or alter any portion of this comment or credits
* of supporting developers from this source code or any supporting source code
* which is considered copyrighted (c) material of the original comment or credit authors.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright Hervé Thouzard of Instant Zero (http://www.instant-zero.com)
* @license http://www.fsf.org/copyleft/gpl.html GNU public license
* @package oledrion
* @author Hervé Thouzard of Instant Zero (http://www.instant-zero.com)
*
* Version : $Id:
* ****************************************************************************
*/
/**
* Chargement des handlers utilisés par le module
*/
class oledrion_handler
{
/**
* Contient la liste des handlers disponibles
*
* @var array
*/
private $handlersNames = array('oledrion_manufacturer', 'oledrion_products', 'oledrion_productsmanu', 'oledrion_caddy', 'oledrion_cat', 'oledrion_commands', 'oledrion_related', 'oledrion_vat', 'oledrion_votedata', 'oledrion_discounts', 'oledrion_vendors', 'oledrion_files', 'oledrion_persistent_cart', 'oledrion_gateways_options');
/**
* Contient l'unique instance de l'objet
* @var object
*/
private static $instance = false;
/**
* Réceptacle des handlers
*
* @var array
*/
public static $handlers = null;
/**
* Méthode chargée de renvoyer les handlers de données en les chargeant à la volée
*
* @param string $name
* @return mixed Null si on échoue, sinon l'objet demandé
*/
function __get($name)
{
if(substr($name, 0, 2) != 'h_') {
return null;
}
if(!in_array(substr($name, 2), $this->handlersNames)) {
return null;
}
if(!isset($this->handlersNames[$name])) {
$this->handlers[$name] = xoops_getmodulehandler(substr($name, 2), OLEDRION_DIRNAME);
}
return $this->handlers[$name];
}
private function __construct()
{
$this->handlers = array();
}
/**
* Retourne l'instance unique de la clanss
*
* @return object
*/
public static function getInstance()
{
if (!self::$instance instanceof self) {
self::$instance = new self;
}
return self::$instance;
}
}
?>
Note, this class requires Php 5.