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   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-07-13 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 34 All time: 11,016 This week: 43Up
Version License PHP version Categories
minimal-singleton 1.0.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
Age: ???
All time rank: 226411 in Nigeria Nigeria
Week rank: 163 Up5 in Nigeria Nigeria Up
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 (5)  
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 (5)  /  src  
File Role Description
  Plain text file Environment.php Class Class source
  Plain text file SingletonHelper.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:34
This week:0
All time:11,016
This week:43Up