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.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Calculator.php
Greeting.php
GreetingClass.php
README.md
index.php

README.md

Create a PHP Singleton class

This is the source code that goes along with the video 'The PHP Singleton Class', located at https://devdojo.com/episode/the-php-singleton-class

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:

<?php

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;
    }

}