\donbidon\Lib\PHPUnitT_ResetInstance

Implements functionality allowing to reset singleton instance.

class Foo
{
    use \donbidon\Lib\PHPUnit\T_ResetInstance;

    protected static $myInstance;

    public static function getInstance()
    {
        if (!is_object(self::$myInstance)) {
            self::$myInstance = new self;
        }

        return self::$myInstance;
    }

    protected function __construct()
    {
        self::setInstancePropertyName('myInstance');
        $this->allowToResetInstance();

        echo sprintf("%s called%s", __METHOD__, PHP_EOL);
    }
}

Foo::getInstance();
Foo::resetInstance();
Foo::getInstance();

outputs

Foo::__construct() called
Foo::__construct() called

Summary

Methods
Properties
Constants
resetInstance()
No public properties found
No constants found
setInstancePropertyName()
allowToResetInstance()
No protected properties found
N/A
No private methods found
$instancePropertyName
$allowToResetInstance
N/A

Properties

$instancePropertyName

$instancePropertyName : string

Singleton instance property name

Type

string

$allowToResetInstance

$allowToResetInstance : boolean

Allow to reset singleton instance flag

Type

boolean

Methods

resetInstance()

resetInstance() : void

Resets static instance.

Throws

\RuntimeException

If resetting of instance disallowed

setInstancePropertyName()

setInstancePropertyName(string  $name) : void

Sets instance property name.

Parameters

string $name

allowToResetInstance()

allowToResetInstance(boolean  $allow = TRUE) : void

Allows or disallows to reset instance.

Parameters

boolean $allow