PHP Classes

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    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 142 All time: 9,178 This week: 660Up
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.

Picture of Mohamed Elbahja
  Performance   Level  
Name: Mohamed Elbahja is available for providing paid consulting. Contact Mohamed Elbahja .
Classes: 12 packages by
Country: Morocco Morocco
Age: 29
All time rank: 9041 in Morocco Morocco
Week rank: 360 Up3 in Morocco Morocco Down
Innovation award
Innovation award
Nominee: 4x

Documentation

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:142
This week:0
All time:9,178
This week:660Up