-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated use of singleton to ensure classes get used
- Loading branch information
1 parent
8e09f89
commit 93afef5
Showing
3 changed files
with
34 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,46 @@ | ||
<?php | ||
/** | ||
* Singleton definition for this plugin's classes | ||
* Singleton trait | ||
* | ||
* @package Satellite | ||
*/ | ||
|
||
namespace Eighteen73\Satellite; | ||
|
||
/** | ||
* Abstract class | ||
*/ | ||
abstract class Singleton { | ||
trait Singleton { | ||
|
||
/** | ||
* Return instance of class | ||
* The class instance | ||
* | ||
* @return self | ||
* @var self|null | ||
*/ | ||
public static function instance(): Singleton { | ||
static $instance; | ||
|
||
if ( empty( $instance ) ) { | ||
$class = get_called_class(); | ||
private static $instance = null; | ||
|
||
$instance = new $class(); | ||
|
||
if ( method_exists( $instance, 'setup' ) ) { | ||
$instance->setup(); | ||
} | ||
/** | ||
* Get the current instance | ||
* | ||
* @return Singleton | ||
*/ | ||
final public static function instance(): self { | ||
if ( self::$instance === null ) { | ||
self::$instance = new self(); | ||
} | ||
return self::$instance; | ||
} | ||
|
||
/** | ||
* Constructor | ||
*/ | ||
private function __construct() { | ||
// Intentionally empty | ||
} | ||
|
||
return $instance; | ||
/** | ||
* Class clone | ||
* | ||
* @return void | ||
*/ | ||
private function __clone() { | ||
// Intentionally empty | ||
} | ||
} |