This is the source code creating in the video 'The PHP Singleton Class'
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Create a PHP Singleton class

This is the source code that goes along with the video 'The PHP Singleton Class', located at

In this video you'll learn how to create a PHP singleton class and when and why you may want to use the Singleton pattern.

Create a PHP Singleton

Below is an example of a very simple Calculator Singleton Class:


class Calculator{

    private static $instance;

    public static function singleton(){

        if( !isset(self::$instance) ){
            self::$instance = new Greeting;

        return self::$instance;


    public static function add($num1, $num2){
        return (int)$num1 + (int)$num2;