Created
February 21, 2011 16:17
-
-
Save gcoop/837282 to your computer and use it in GitHub Desktop.
Singleton Pattern in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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