PHP Register Mode Class Usage example analysis, registrar instance Analysis _php Tutorial

Source: Internet
Author: User

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.