Once
- Object
- Once
A Once class creates only one instance.
CLASS METHODS
- namespace Once;
- require_once 'So-o.php';
- defclass('Once', null, 1, array('instance'), null, array('new'), null);
The Once class inherits from the Object class. The class property instance keeps the unique instance of the class. The Once class redefines the class message new.
new
SYNOPSIS
sendmsg($class, 'new'[, $arg ...])
DESCRIPTION
The first time new is called, it returns a new instance of $class
.
The following times, it returns this same instance of $class
.
new passes all the $arg
parameters of the message to init.
CODE
- namespace Once;
- require_once 'So-o.php';
- defclass('Once', null, 1, array('instance'), null, array('new'), null);
The Once class inherits directly from the Object class. It keeps the only instance it creates in the class property instance. It redefines the class message new.
- function c_new($self) {
- $i=sendmsg($self, 'get', 'instance');
- if (!$i) {
- $i=supersend('new', func_get_args());
- sendmsg($self, 'set', 'instance', $i);
- }
- return $i;
- }
new
retrieves the class property instance.
If this property has no value yet, new
creates a new instance by calling the new
method inherited from the Object class with all the arguments of the message and saves the new instance in the class property instance.
new
returns the instance which was saved or the new instance.
Comments