PHP in the Register mode class Usage example Analysis _php skill

Source: Internet
Author: User

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.

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.