ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing

ExpertRating Home
ExpertRating Benefits
Recommend ExpertRating
Suggest More Tests
Privacy Policy
PHP Tutorial - Index
PHP Tutorial - Introduction to PHP
PHP Tutorial - Your First PHP Script
PHP Tutorial - Operators
PHP Tutorial - Conditional and Looping
PHP Tutorial - Functions
PHP Tutorial - Classes and Objects
PHP Tutorial - Working with Forms
PHP Tutorial - Advanced PHP Topics
PHP Tutorial - Advanced Class Concepts

Online Personal Trainer Certification
Personal Trainer Certification (Spanish)
Six Sigma Certification
Find a Personal Trainer
Project Management Certification
Six Sigma Certification
Six Sigma Black Belt Certification
Master Black Belt Certification
Six Sigma Green Belt Certification
Life Coach Certification
Lean Management Certification
Lean Six Sigma Certification
First Aid Certification
CPR Certification
Yoga Certification
Aerobics Certification
Pilates Certification
Business Analyst Certification
SEO Certification
Online Business Writing Course & Certification
Makeup Artist Certification
Baby Sitting Course & Certification
Time Management Certification
Health Club Management Certification
Medical Coding Certification
Medical Billing Certification
Business Analysis Course
Master Trainer Certification
Total Quality Management Certification
Kaizen Certification
Creative Writing Certification
Event Management Certification
Test Search by Job Title
Administrative Assistant Certification
Virtual Assistant Certification
  All Online Courses


  Home > Courses, Tutorials & eBooks > PHP Tutorial >Advanced Class Concepts

PHP Tutorial - Advanced Class Concepts

Page 2 of 3

Singleton Pattern

The singleton pattern applies to those situations in which there needs to be a single instance of a class. Implementing this pattern makes this single instance easily accessible by many other objects.



class Example


// Hold an instance of the class

private static $instance;

// A private constructor; prevents direct creation of object

private function __construct()


echo 'I am constructed';


// The singleton method

public static function singleton()


if (!isset(self::$instance)) {

$c = __CLASS__;

self::$instance = new $c;


return self::$instance;


// Example method

public function bark()


echo 'Woof!';


// Prevent users to clone the instance

public function __clone()


trigger_error('Clone is not allowed.', E_USER_ERROR);





Magic Methods

The function names _construct, _sleep, _wakeup, _tostring are all magical methods in PHP. There cannot be any functions with these names in the classes. These functions can be had only if we want the magical functionality associated with them.


The _sleep magic method is used to close any database connections that the object may have. It is also used to commit pending data and perform similar cleanup tasks. It is also used if there are very large objects which do not have to be saved completely.


The _wakeup magic method is used to reconstruct any resources that the object may have. The basic function of _wakeup is to reestablish any database connections that may have been lost during serialization and perform other initialization tasks.

Example of sleep and wakeup


class Conn {

protected $link;

private $server, $username, $password, $db;

public function __construct($server, $username, $password, $db)


$this->server = $server;

$this->username = $username;

$this->password = $password;

$this->db = $db;



private function connect()


$this->link = mysql_connect($this->server, $this->username, $this->password);

mysql_select_db($this->db, $this->link);


public function __sleep()









Home  |  About Us  |  Privacy Policy  |  Site Map  |  FAQs  |  Contact Us
© ExpertRating 2015. All Rights Reserved.