PHP Classes

How to Implement the PHP Singleton Design Pattern to Ensure That Only One Object of a Class Exists Using the Package Minimal Singleton: Show how to create only one object of class

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-07-13 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: 21 This week: 21All time: 11,227 This week: 6Up
Version License PHP version Categories
minimal-singleton 1.0Shareware8Language, Design Patterns, PHP 8
Description 

Author

This package can show how to create only one object of class.

It provides a singleton helper class that can store a single object of a given class and return the same object every time it is called to return the singleton.

The package also provides an example class that uses the singleton helper class to ensure that only one object of that class exists.

Picture of Ahmad Mustapha
Name: Ahmad Mustapha <contact>
Classes: 24 packages by
Country: Nigeria Nigeria
Innovation award
Innovation award
Nominee: 9x

Example

<?php

use Ahmard\Singleton\Environment;

require_once
__DIR__ . '/vendor/autoload.php';

$app = Environment::getInstance()
    ->
setName('Test')
    ->
setEnv('dev')
    ->
setHost('http://localhost');


var_dump(Environment::getInstance()->getAppName());
var_dump(Environment::getInstance()->getAppEnv());
var_dump(Environment::getInstance()->getAppHost());


Details

PHP Singleton

Minimal PHP Singleton Demostration

Installation

Clone the repo

git clone https://github.com/ahmard/minimal-singleton.git

Navigate to the cloned repo

cd minimal-singleton

Install Composer packages

composer install

Usage

Run below command to run the example

php entry.php


  Files folder image Files  
File Role Description
Files folder imagesrc (2 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file entry.php Example Example script
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file Environment.php Class Class source
  Plain text file SingletonHelper.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:21
This week:21
All time:11,227
This week:6Up