Book Image

Object-Oriented Programming with PHP5

By : Hasin Hayder
Book Image

Object-Oriented Programming with PHP5

By: Hasin Hayder

Overview of this book

<p>Some basic objected-oriented features were added to PHP3; with PHP5 full support for object-oriented programming was added to PHP. Object-oriented programming was basically introduced to ease the development process as well as reduce the time of development by reducing the amount of code needed. OOP can greatly improve the performance of a properly planned and designed program.</p> <p>This book covers all the general concepts of OOP then shows you how to make use of OOP in PHP5, with the aid of an ample number of examples.</p>
Table of Contents (15 chapters)
Object-Oriented Programming with PHP5
Credits
About the Author
About the Reviewers
Introduction
Index

Singleton Pattern


One of the most used design patterns is Singleton. This pattern solves a very significant problem in object oriented programming and saves the lives of millions of programmers in practical programming.

The main purpose of the Singleton pattern is to deliver a single instance of object no matter how many times you instantiate it. That is, if an object is instantiated once, using the Singleton pattern you can deliver only that instance when you require it again in your code. This saves memory consumption by preventing the creation of multiple instances of an object. Thus Singleton pattern is used to improve the performance of your application.

Let's take the MySQLManager class, which we created in the previous example. Now we are adding a single instance feature using Singleton pattern.

<?
class MySQLManager
{
  private static $instance;

  public function __construct()
  {
    if (!self::$instance)
    {
      self::$instance = $this;
      echo "New Instance\n";
   ...