Improved singleton implementation

This commit is contained in:
Sam Mousa 2018-11-19 14:27:42 +01:00
parent 5c88c8ac2a
commit 1a5f38f6de
No known key found for this signature in database
GPG Key ID: A18520F9B4301C9D
4 changed files with 12 additions and 5 deletions

View File

@ -1,6 +1,8 @@
<?php <?php
class FileDownloader extends FileManager { class FileDownloader extends FileManager {
use SingletonTrait;
public function download() { public function download() {
$fullFilePath = $this->getFullFilePath(); $fullFilePath = $this->getFullFilePath();

View File

@ -1,7 +1,6 @@
<?php <?php
abstract class FileManager { abstract class FileManager {
use SingletonTrait;
private $fileName; private $fileName;
private $localPath = 'files/'; private $localPath = 'files/';

View File

@ -1,13 +1,14 @@
<?php <?php
class FileUploader extends FileManager { class FileUploader extends FileManager {
use SingletonTrait;
private $maxSize = 1; private $maxSize = 1;
private $fileName; private $fileName;
private $permission; private $permission;
private $storage; private $storage;
protected function __construct() { private function __construct() {
parent::__construct();
$this->storage = new \Upload\Storage\FileSystem($this->getLocalPath()); $this->storage = new \Upload\Storage\FileSystem($this->getLocalPath());
} }

View File

@ -1,10 +1,15 @@
<?php <?php
/**
* Trait SingletonTrait
* This trait helps implementing singleton classes
* It must be used on each class in a hierarchy that needs to be a singleton
*/
trait SingletonTrait trait SingletonTrait
{ {
private static $instance; protected static $instance;
protected function __construct() private function __construct()
{ {
} }