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

Factory Pattern


Another common design pattern is factory pattern. The main goal of this pattern is delivering an object by hiding all the complexities behind it. This may sound cryptic, so let's look at it using a real life scenario.

You are doing a project that works on a very complex system. For this example, you are creating an online document repository, which saves documents in temporary storage. For this you need support for PostgreSQL, MySQL, Oracle, and SQLite because users may deploy your application using any of these. So you create an object, which connects to MySQL and perform the necessary tasks. Your MySQL object is:

<?
class MySQLManager
{
  public function setHost($host)
  {
    //set db host
  }

  public function setDB($db)
  {
    //set db name
  }
  public function setUserName($user)
  {
    //set user name
  }
  public function setPassword($pwd)
  {
    //set password
  }

  public function connect()
  {
    //now connect
  }
}
s
?>

Well, now you use this class...