source: OWR/Singleton.php @ 22257f

Revision 22257f, 3.1 KB checked in by pierre-alain <pierre-alain@…>, 9 years ago (diff)

v0.1a2

  • Property mode set to 100644
Line 
1<?php
2/**
3 * Abstract class for singleton pattern
4 *
5 * PHP 5
6 *
7 * OWR - OpenWebReader
8 *
9 * Copyright (c) 2009, Pierre-Alain Mignot
10 *
11 * Home page: http://openwebreader.org
12 *
13 * E-Mail: contact@openwebreader.org
14 *
15 * All Rights Reserved
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 *
31 * @author Pierre-Alain Mignot <contact@openwebreader.org>
32 * @copyright Copyright (c) 2009, Pierre-Alain Mignot
33 * @license http://www.gnu.org/copyleft/gpl.html
34 * @package OWR
35 * @abstract
36 */
37namespace OWR;
38use OWR\Interfaces\Singleton as iSingleton;
39/**
40 * This object is used to implement the singleton pattern
41 * @abstract
42 * @package OWR
43 */
44abstract class Singleton implements iSingleton
45{
46    /**
47    * @var array store instancied objects
48    * @access private
49    * @static
50    */
51    private static $_instances = array();
52
53    /**
54     * Constructor
55     *
56     * @access protected
57     */
58    protected function __construct() {}
59
60    /**
61     * Instance getter
62     * This function can NOT be overloaded
63     *
64     * @access public
65     * @static
66     * @author Pierre-Alain Mignot <contact@openwebreader.org>
67     * @return mixed the instance
68     */
69    final static public function iGet() 
70    {
71        $c = get_called_class();
72        if(!isset(self::$_instances[$c]))
73        {
74            self::$_instances[$c] = new $c(func_get_args());
75        }
76
77        return self::$_instances[$c];
78    }
79
80    /**
81     * Cloning is denied
82     * This function can NOT be overloaded
83     *
84     * @access public
85     * @author Pierre-Alain Mignot <contact@openwebreader.org>
86     */
87    final public function __clone()
88    {
89        throw new Exception('Please edit your code, cloning a singleton is not allowed');
90    }
91
92    /**
93     * Executed when an object is unserialized
94     * We register the new object
95     *
96     * @access public
97     * @author Pierre-Alain Mignot <contact@openwebreader.org>
98     */
99    public function __wakeUp()
100    {
101        self::register($this);
102    }
103
104    /**
105     * Register function
106     * This function is called when an object is unserialized and is used to register the instance of the object
107     * This function can NOT be overloaded
108     *
109     * @access public
110     * @static
111     * @author Pierre-Alain Mignot <contact@openwebreader.org>
112     */
113    final static public function register($instance)
114    {
115        if(!is_object($instance)) return false;
116
117        $c = get_class($instance);
118        if(isset(self::$_instances[$c]))
119            return false;
120
121        self::$_instances[$c] = $instance;
122    }
123}
Note: See TracBrowser for help on using the repository browser.