PHP Classes
elePHPant
Icontem

Singleton Class: Create, retrieve an delete instances of a class

Recommend this page to a friend!
  Info   View files Documentation   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-07-22 (1 year ago) RSS 2.0 feedNot enough user ratingsTotal: 136 All time: 8,722 This week: 376Up
Version License PHP version Categories
singletonclass 1.0.1Custom (specified...7PHP 5, Language
Description Author

This class can create, retrieve an delete instances of a class.

It can create a unique instance of a given class, so next time an instance of the class is requested, it will return the same instance.

The class can also discard the instance of a class that was previously created.

  Performance   Level  
Name: Mohamed Elbahja <contact>
Classes: 10 packages by
Country: Morocco Morocco
Innovation award
Innovation award
Nominee: 4x

Details

Singleton

get objects instances by singleton class

Examples

Get Instances


require_once('path/to/Singleton.php');

class Test extends Singleton
{
  public $test;

  // code
}

$test1 = Test::getInstance();
$test1->test = 'test';

function testGetInstance() {
  $test = Test::getInstance();
  echo PHP_EOL;
  var_dump($test->test);
}

testGetInstance();

// output:
// string(4) "test"

Get Instances By Reference


require_once('path/to/Singleton.php');

class Test extends Singleton
{

  public $test;

}

$test = &Test::getInstance();
$test->test = 'test';

function testCallback() {
  $test = &Test::getInstance();
  $test = null;
}
echo PHP_EOL;
var_dump($test);
testCallback();
echo PHP_EOL;
var_dump($test);

// output:
// object(Test)#1 (1) {
//   ["test"]=>
//   string(4) "test"
// }
//
// NULL
  Files folder image Files  
File Role Description
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Plain text file Singleton.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:136
This week:0
All time:8,722
This week:376Up