This example describes the usage of the Registrar mode class in PHP. Share to everyone for your reference, specific as follows:
Register Read and Write class
Registry.class.php
<?php/** * Register read-Write class */class Registry extends Arrayobject {/** * Registry instance * * @var object/P
rivate static $_instance = null;
/** * Obtain Registry instance * * @note single piece Mode * * @return object/public static function getinstance () {
if (self::$_instance = = null) {self::$_instance = new self ();
echo "New Register object!";
return self::$_instance; /** * Save one content to the registry * * @param string $name index * @param mixed $value data * * @return void/Pub
Lic static function set ($name, $value) {self::getinstance ()->offsetset ($name, $value); /** * Gets the value of an item in the registry * * @param string $name Index * * @return Mixed/public static function get ($n
AME) {$instance = Self::getinstance ();
if (! $instance->offsetexists ($name)) {return null;
Return $instance->offsetget ($name); /** * Check if an index exists * * @param string $name Index * * @reTurn Boolean/public static function isregistered ($name) {return self::getinstance ()->offsetexists ($name)
; /** * Deletes the specified entry in the registry * * @param string $name index * * @return void/public static function remove ($n
AME) {self::getinstance ()->offsetunset ($name);
}
}
Classes that need to be registered
test.class.php
<?php
class Test
{
function hello ()
{
echo "Hello World";
return;
}
? >
Test test.php
<?php
//introduction of related class
require_once "Registry.class.php";
Require_once "test.class.php";
New A object
$test =new test ();
$test->hello ();
Registered Object
registry::set (' TestClass ', $test);
Remove Object
$t = Registry::get (' TestClass ');
Call object Method
$t->hello ();
? >
I hope this article will help you with the PHP program design.