Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
class ClassThatMarksDateOfInstantiation
{
private $dob;
public function __construct()
{
$this->dob = new \DateTimeImmutable;
}
public function getDob()
{
return $this->dob;
}
}
$factoryOfDobClass = function () {
return new ClassThatMarksDateOfInstantiation;
};
$DobSingleton = singletonize($factoryOfDobClass); // -> anonymous class
$dob = new $DobSingleton; // -> new instance of anonymous class
$dob2 = new $DobSingleton; // -> same instance of anonymous class
print_r($dob->getDob() === $dob2->getDob()); // -> 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment