Top  Branding  Banner 
blank.gif
blank.gif
triangle.gif Du er her: /  Forsiden  /  Kildekoden  /  Basic  /  Singleton   Login nu   Login
blank.gif
««« Se kilde koden
blank.gif
tl.gif Base tr.gif tls.gif     Basic  trs.gif tl.gif Dto tr.gif tl.gif Form tr.gif tl.gif Language tr.gif tl.gif Layout tr.gif tl.gif Menu tr.gif tl.gif Mvc tr.gif tl.gif Netbank.eksperter.dk tr.gif tl.gif Tab tr.gif tl.gif Table tr.gif tl.gif Util tr.gif
blank.gif
blank.gif
arrow-headline.gif Index
MenuLink  MenuLeft  
Tilbage

Skjul: Navn

Singleton.php


Vis: Sample code, tutorial

Singleton, Sample code, tutorial

Sådan benyttes komponenten Singleton klassen

Først skal du inkludere den fil der beskriver komponenten, som en klasse fil

  • <?
    require_once(HTML_PACKAGE_PATH.'/Singleton.php');
    ?>

Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):

  • <?
    Singleton
    ::display($param1$param2$param3, ...);
    ?>

eller du kan lave en instance af komponenten og benytte metoderne direkte:

  • <?
    $object 
    = new Singleton($param1$param2$param3, ...);
    print 
    $object->getHtml();
    ?>

Skjul: Sådan vises komponenten

Singleton, Sådan vises komponenten

Sådan vises komponenten Singleton klassen

Connection (key)=Connection
Buffer (key)=Buffer

Vis: PHP source code

Singleton, PHP source code

Den fulde PHP kildekode for Singleton klassen

<?php
/**
 * @package basic
 * @see HTML_BASIC_UTIL_PATH.'/Singleton.php'
 * @copyright (c) http://Finn-Rasmussen.com
 * @license http://Finn-Rasmussen.com/license/ myPHP License conditions
 * @author http://Finn-Rasmussen.com
 * @version 1.11
 * @since 27-nov-2009
 */

/**
 * The required files
 */
if (defined('HTML_LOG_UTIL_PATH')) {
    require_once(
HTML_LOG_UTIL_PATH.'/Log.php');
}

/**
 * The Singleton Factory class used in the myPHP
 * <code>
 * Usage:
 *   $instance = & Singleton::getInstance($class, $id);
 * Or
 *   Singleton::getInstance(); // Dump all singleton classes
 * </code>
 * @package basic 
 */
class Singleton {
    
/**
     * Constructor
     * @private
     */
    
function __construct() {
    }
    
    
/**
     * Get an instance of the specified class
     * The singleton pattern is applied
     * It requires that the class definition be pre-loaded as require_once. 
     * If the file name could be deduced or constructed from the class name 
     * then it could be built into the getInstance() method.
     * Return all the singleton classes, if no class and id is specified
     * @param  String $class The name of the class
     * @param  String $id Adding an optional ID allows holding more than one object a class. Assigned ID will distinguish array keys.
     * @return Object The one and only single instance of the object 
     */
    
public static function & getInstance($class=''$id='') {
        
/**
         * array of instance names 
         */
        
static $instances = array ();
        
$key $class.$id;
        if (
$key != '') {
            if (
array_key_exists($key$instances)) {
                
// Ok exists
            
} else {
                
// instance does not exist, so create it
                
$instances[$key] = new $class();
            }
            
$instance = & $instances[$key];
            return 
$instance;
        } else {
            
/**
             * Dump all the singleton classes
             * Debug only
             */
            
if (defined('DEBUG_LEVEL_SHOW_CLASS') && DEBUG_LEVEL DEBUG_LEVEL_SHOW_CLASS) {
                
$html "";
                if (
defined('HTML_BASE_UTIL_PATH')) {
                     foreach (
$instances as $key=>$value) {
                         
$html .= "$key (key)=".ucfirst(get_class($value))."<br />\r\n";
                     }
                } else {
                    
$html .= "Not possible<br />\r\n";
                }
                 return 
$html;
            } else {
                
// Ignore
                
$log = new Message();
                 return 
$log;
            }
        }
    }
    
    
/**
     * Get the html code
     * @return String The html code
     */
    
function getHtml() {
        return 
Singleton::getInstance();
    }
}
?>

Vis: HTML source code

Singleton, HTML source code

Den fulde HTML kildekode for Singleton klassen

<?
Connection 
(key)=Connection<br />
Buffer (key)=Buffer<br />

?>

Vis: Class methods

Singleton, Class methods

Her er 'klasse metoderne' for Singleton klassen:

  • __construct
  • getInstance
  • getHtml

Vis: Object vars

Singleton, Object vars

Her er 'objekt variable' for Singleton klassen:


MenuRight 
triangle.gif

Dansk

Deutch

English (UK)

France

Italy

Norsk

Svensk

English (USA)


 
blank.gif
MenuBottom 
triangle.gif Copyright @ 1999-2010 www.Finn-Rasmussen.com Powered by myPHP Version (5.3.3-7+squeeze3) 1.11
blank.gif