PHP Classes

Mezon PHP Singleton Class: Create object of a class that only has 1 instance

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 31 All time: 11,122 This week: 206Up
Version License PHP version Categories
mezon-singleton 1.0MIT/X Consortium ...5PHP 5, Language, Design Patterns
Description 

Author

This class can be used to create object of a class that only has 1 instance.

Applications need to create a sub-class of this class to make that class only have one instance of the sub-class.

Attempts to create new objects of the same class after when the first instance of that class is created, will return the same object.

The class also provides a function to destroy the only instance of the singleton sub-class.

Picture of Alexey Dodonov
  Performance   Level  
Name: Alexey Dodonov <contact>
Classes: 58 packages by
Country: Russian Federation Russian Federation
Age: ???
All time rank: 184854 in Russian Federation Russian Federation
Week rank: 128 Up9 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 13x

Documentation

Singleton

Intro

This class implements basic singleton pattern.

Usage

Using singleton behaviour in your applications is so simple as it can be. Just extend the class Singleton, and enjoy using it!

See the code:

// including all necessary files
require_once( dirname( dirname( dirname( __FILE__ ) ) ).'/conf/conf.php' );
require_once( $MEZON_PATH.'/vendor/singleton/singleton.php' );

// creatingyour own class
class   MyClass extends Singleton
{
    // methods and their implementations
}

That's all!

You can see the Template Engine class for real usage of this pattern.


  Files folder image Files (5)  
File Role Description
Files folder imageTests (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Plain text file Singleton.php Class Class source

  Files folder image Files (5)  /  Tests  
File Role Description
  Plain text file SingletonUnitTest.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:31
This week:0
All time:11,122
This week:206Up