Skip to content

Instantly share code, notes, and snippets.

@gcoop
Created February 21, 2011 16:17
Show Gist options
  • Save gcoop/837282 to your computer and use it in GitHub Desktop.
Save gcoop/837282 to your computer and use it in GitHub Desktop.
Singleton Pattern in PHP
<?php
/**
* PHP Singleton pattern (same as Java singleton).
*
* Analytics::incrementView(this); // Within the photo class for example.
*/
class Analytics
{
private static $instance;
private function __construct() { }
public static function inst() {
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function incrementView($obj)
{
// Take the object and increment the views for just this object.
}
public function saveIncrementedViews()
{
// Do all the saves in one go.
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment