Example analysis of Registrar pattern class usage in PHP, instance analysis of Registrar
This example describes the usage of the Registrar pattern class in PHP. Share to everyone for your reference, as follows:
Registrar read-Write class
Registry.class.php
<?php/** * Registrar read-write class */class Registry extends arrayobject{/** * Registry Instance * @var Object */Private Stati c $_instance = null; /** * Get Registry instance * * @note single-piece mode * * @return Object */public static function getinstance () {if :: $_instance = = = null) {self::$_instance = new self (); echo "New Register object!"; } return self::$_instance; /** * Save a content into the registry * * @param string $name index * @param mixed $value data * * @return void */public Stati C 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 ($name) { $instance = Self::getinstance (); if (! $instance->offsetexists ($name)) {return null; } return $instance->offsetget ($name); }/** * Checks if an index exists * * @param string $name Index * * @return Boolean */public static function isregistered ( $name) {return self::getinstance ()->offsetexists ($name); /** * Delete the specified item in the registry * * @param string $name index * * @return void */public static function remove ($name) { Self::getinstance ()->offsetunset ($name); }}
Classes that need to be registered
test.class.php
<?phpclass test{ function Hello () { echo "Hello World"; return;} } ?>
Test test.php
<?php//introduced the related class require_once "Registry.class.php"; require_once "test.class.php";//new a object$test=new test ();//$ Test->hello ();//Register Object Registry::set (' TestClass ', $test);//Remove Object $t = Registry::get (' TestClass ');//Call object method $t-> Hello ();? >
I hope this article is helpful to you in PHP programming.
http://www.bkjia.com/PHPjc/1067836.html www.bkjia.com true http://www.bkjia.com/PHPjc/1067836.html techarticle PHP in the Register mode class usage instance analysis, the Registrar instance analysis This article describes the PHP in the Registrar pattern class usage. Share to everyone for your reference, as follows: Registrar read ...