PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Mohamed Elbahja  >  Singleton Class  >  README.md  >  Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: Singleton Class
Create, retrieve an delete instances of a class
Author: By
Last change: update
Date: 4 years ago
Size: 839 bytes
 

Contents

Class file image Download

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
For more information send a message to info at phpclasses dot org.