PHP Singleton

Singleton Design Pattern for PHP5.3

I would like to share a very simple Singleton class with you. I’ve created this long time ago, but since I decided to continue blogging again, I will just copy paste this with some explanation to make a start at blogging again:

First take a look at the code, then read the explanation that follows.

<?php
/**
* Very simple Singleton object
*
* @author Dino Hensen
*
*/
abstract class Singleton
{
/**
* Member variable holding an Singleton instance
*
* @var Singleton
*/
protected static $_instance = null;
/**
* Returns new or existing instance of Singleton.
*/
final public static function getInstance()
{
if(null === self::$_instance) {
self::$_instance = new static();
}
return self::$_instance;
}
/**
* Prevents external object creation
*/
final private function __construct() { }
/**
* Prevents object cloning
*/
final private function __clone() { }
}
?>

Singleton code Explanation

A class for which you want only one instance to exist at all times is very common in software systems. It is called a Singleton!
A Singleton design pattern ensures that it is never possible to create more than one instance of a class that extends the Singleton class defined below.

By using a private constructor you ensure that no other class will be able to create an instance of Singleton class. By adding final to all functions you ensure that no extending class will be able to override this functionality and thus hack your Singleton class.

The getInstance function works as follows: the first time the function is called a Singleton object is instantiated for which protected static $_instance holds a reference to this object. The next time you call getInstance the instance already exists so the reference to the instance is returned. Remember, PHP5 does not pass objects by-value anymore, but instead passes by-reference.

Notice

The Singleton code relies on using PHP5.3 because of something called late static binding.

self::$_instance = new static();

new static() in PHP 5.3’s late static binding refers to whatever class in the hierarchy which you call the method on. We need this because PHP needs to know which object to instantiate, that is a ‘Singleton’ or ‘Boss’ object. You can read more about late static bindings at: http://www.php.net/manual/en/language.oop5.late-static-bindings.php

Usage

And this is how you use it:

<?php
require_once "singleton.class.php";
class Boss extends Singleton {
/**
* Returns a bossy text
* @return string
*/
public function whoIsTheBoss()
{
return "I am the boss!";
}
}
$boss = Boss::getInstance();
echo $boss->whoIsTheBoss();	//I am the boss!
?>

You can create a new class which extends Singleton and give it some functionality. Then you can create the object using the public static getInstance function which returns a reference to a Boss object. You now safe the reference in the variable named $boss. When running the code it shows you who’s the boss!

PHP Tutorial 2 – Basic syntax

Hello this is tutorial #2!

This tutorial will teach you basic PHP syntax. Any PHP scripting block always starts with <?php and ends with ?>, although some server configurations allow <? for a starting tag, I recommend always using <?php as a starting tag for maximum compatibility. So we always start and end like this:

<?php
//some code here...
?>

In my code fragment above you might have noticed ‘//some code above‘ The two forward slashes mean a comment is following. This kind of comment is called a single-line comment and is terminated when you insert a newline character.
For multi-line-comment use this construction:

<?php
/* Multi Line Comment started...
You can continue on this line...
and even on this line...
But after the multi line comment stop, things you type will be interpreted as code again
*/
echo "This is a little bit of code that will be executed";
// echo "This line wont be executed because it's commented!";
// So after the */ you can type code again
?>

So now that you know basic php syntax for starting and ending a php scripting block, I will finally introduce you to using variables! Continue reading PHP Tutorial 2 – Basic syntax

PHP Tutorial 1 – Getting started with PHP using WampServer2 on Windows 7

Hello all,

Here is the first of tutorials that I will make to cover the basics of PHP. My goal is not to have yet another tutorial-blog/site, but to show how I got started with PHP and eventually show some advanced things you can do with it. To reach that goal, I first want to introduce ‘newbies’ to PHP. In this tutorial I’m going to assume you have basic knowledge of HTML, which will be used sometimes in examples. If you don’t know HTML, I recommend visiting w3schools.com

What is PHP?

PHP is a scripting language which is used among others for creating dynamic web pages.

What does PHP stand for?

PHP stands for: Hypertext Preprocessor. You might ask me what the first P stands for… well, back in the days PHP used to mean: Personal Home Page, but then they wanted something more sophisticated and changed it a little while keeping the first P.

What can you do with it?

There infinitely many things you could do with PHP, but it comes down to this list of general subjects:

  • create dynamic web page
  • create web application
    • PhpMyAdmin (database management system)
  • create a CMS(content management system)
    • WordPress
    • Drupal
    • Joomla
  • create <script_name_here>

Continue reading PHP Tutorial 1 – Getting started with PHP using WampServer2 on Windows 7