Registry

From EQdkp Plus
Jump to navigation Jump to search


Since late-alpha of EQdkp-Plus 1.0 we use a registry. The registry is a static class, which task is to ensure global availability of classes and constants. Another important point is, that the registry takes care of the destruction process, so you can safely use other classes (e.g. database, datacache) in your destruct method, without the risk of getting a fatal error, because the class used has been destroyed before.

Public methods of registry

These methods are needed, whenever you want to access global classes/constants. Usually you don't need to call these directly, because you are operating in a class which is extended from the gen_class. You call these methods in the following way:

$example = registry::register(params);

register

You call the register method when you want to get an instance of a class. For most classes, you don't need to care for its existance. The complete list can be found in the core/super_registry.class.php in the $locs array. For all other classes you have to ensure, that the class is included, but not instantiated before.

Parameters

registry::register($classname, $params=array());
  • $classname is the name of the class you want to get the instance of
  • $params is an array of params you want to pass to the constructor of the class

Return-Value

You get the instance of the class returned. For most classes this instance is cached (see this for details). To get the same instance again you need to pass exactly the parameters.

Example

//old
$file_handler = new file_handler(false);
$another_file_handler = new file_handler(true);
//new
$file_handler = registry::register('file_handler', array(false));
//using the same instance again ($file_handler->do_something() would be a lot easier)
registry::register('file_handler', array(false))->do_something();

$another_file_handler = registry::register('file_handler', array(true));

class_exists

Checks if the registry know this class and is able to load it on its own.

Parameters

registry::class_exists($classname);

Return-Value

boolean true/false.

get_const

If you want to get a global constant, e.g. root_path, you call this method. By default there are the constants root_path, SID, SKEY, scriptstart and everything from config.php available.

Parameters

registry::get_const($constname);

add_const

Add a variable as global constant.

Parameters

registry::add_const($name, $value);

isset_const

Checks if a constant is set.

Parameters

registry::isset_const($name);

The gen_class

This class simplifies many things for you. To benefit, you must extend you class with the gen_class.

class MyExample extends gen_class { /* ... */ }

direct extending

If your class can directly extend from the gen_class. In the $shortcuts array you list all the classes you want to use via $this-> in your class. The information in the array is stored as follows:

array(KEY => DATA);

Afterwards you can access the class via $this->KEY. DATA is the classname, if the class needs no arguments passed to the constructor. If the class needs arguments passed to the constructor it's an array:

DATA = array('classname', array($param1, $param2, ...));

For most of the regular used classes there exist short versions. You then just write the global shortform. Example:

$shortcuts = array('pdl', 'pdh', 'core');

You then can access the plus_debug_logger via $this->pdl, the plus_datahandler via $this->pdh and the core-class via $this->core. All short-versions can be found in the $aliases array in core/super_registry.class.php.

Example

class Example extends gen_class {
  public static $shortcuts = array();
}

Additionally there exists a dependencies array. It's build in the same way the shortcuts array is, but you only need to put those classes in there, which you want to use in the __destruct method of your class.

indirect extending

If your class extends from a class which extends from the gen_class. It is nearly everything the same as a direct extended class, except of one point: You don't directly define the $shortcuts array public static, but define a function instead, looking similar to the following Example:

class MyClass extends YourClass {
  public static function __shortcuts() {
    //define the local shortcuts of this class
    $shortcuts = array('core', 'db', 'pdh', 'myshort' => 'someclass');
    return array_merge(parent::$shortcuts, $shortcuts); //note: in some cases you want to use parent::__shortcuts()
  }
}
class YourClass extends gen_class {
  public static $shortcuts = array('core', 'pfh', 'pdl');
}

The purpose here is to ensure that both, the parent as well as the child, are able to have their own dependencies.

singleton

You can define if your class is a singleton or not. Singleton means, you want only one instance of the class to exist. By default classes are singletons, but you can disable it:

class Example extends gen_class {
  public static $singleton = false; //here we define, that our class is no singleton
  /* the class itself */
}

Directly accessed files

If your file is directly accessed (all files which include the common.php itself), you need to write one additional line at the bottom of the file.

<?php
//include common
class MyExample extends gen_class {
/* the class-body */
}
registry::register('MyExample');
?>

Otherwise, nothing will happen.

Lite Mode

The lite mode is the mode, which was originally added, to be used in the maintenance mode. Only very few classes are initiated by default (pdl, db, user, config). You are however not limited to use these classes only. You can still use every class you want.

How to enable

It's very simple to enable, just define $lite = true; before you include the common.php.

$lite = true;
$eqdkp_root_path = './';
include_once($eqdkp_root_path.'common.php');
/* ... */