Design Patterns in PHP

There are number of ways to structure your code and project for your applications. Use of design patterns is usually a good idea to follow common patterns because it will make your code easier to manage and easier for others to understand.

We have five deigns patterns these are:

  • Factory
  • Singleton
  • Strategy
  • Front Controller
  • Model-View Controller

Factory

Factory pattern is most commonly used design patterns. In this pattern, a class simply creates the object you want to use. Consider the following example of the factory pattern:

This code uses a factory to create the Vehicle object. There are two possible benefits to building your code this way; the first is that if you need to change, rename, or replace the Vehicle class later on you can do so and you will only have to modify the code in the factory, instead of every place in your project that uses the Automobile class. The second possible benefit is that if creating the object is a complicated job you can do all of the work in the factory, instead of repeating it every time you want to create a new instance.

Using the factory pattern isn’t always necessary (or wise). The example code used here is so simple that a factory would simply be adding unneeded complexity. However if you are making a fairly large or complex project you may save yourself a lot of trouble down the road by using factories.

Singleton

When designing web applications, it often makes sense conceptually and architecturally to allow access to one and only one instance of a particular class. The singleton pattern enables us to do this.

The code above implements the singleton pattern using a static variable and the static creation method getInstance(). Note the following:

  • The constructor __construct() is declared as protected to prevent creating a new instance outside of the class via the new operator.
  • The magic method __clone() is declared as private to prevent cloning of an instance of the class via the clone operator.
  • The magic method __wakeup() is declared as private to prevent unserializing of an instance of the class via the global function unserialize() .
  • A new instance is created via late static binding in the static creation method getInstance() with the keyword static. This allows the subclassing of the class Singleton in the example.

The singleton pattern is useful when we need to make sure we only have a single instance of a class for the entire request lifecycle in a web application. This typically occurs when we have global objects (such as a Configuration class) or a shared resource (such as an event queue).

You should be wary when using the singleton pattern, as by its very nature it introduces global state into your application, reducing testability. In most cases, dependency injection can (and should) be used in place of a singleton class. Using dependency injection means that we do not introduce unnecessary coupling into the design of our application, as the object using the shared or global resource requires no knowledge of a concretely defined class.

 

Strategy

With the strategy pattern you encapsulate specific families of algorithms allowing the client class responsible for instantiating a particular algorithm to have no knowledge of the actual implementation. There are several variations on the strategy pattern, the simplest of which is outlined below:

This first code snippet outlines a family of algorithms; you may want a serialized array, some JSON or maybe just an array of data:

By encapsulating the above algorithms you are making it nice and clear in your code that other developers can easily add new output types without affecting the client code.

You will see how each concrete ‘output’ class implements an OutputInterface – this serves two purposes, primarily it provides a simple contract which must be obeyed by any new concrete implementations. Secondly by implementing a common interface you will see in the next section that you can now utilise Type Hinting to ensure that the client which is utilising these behaviours is of the correct type in this case ‘OutputInterface’.

The next snippet of code outlines how a calling client class might use one of these algorithms and even better set the behaviour required at runtime:

The calling client class above has a private property which must be set at runtime and be of type ‘OutputInterface’ once this property is set a call to loadOutput() will call the load() method in the concrete class of the output type that has been set.

 

Front Controller

The front controller pattern is where you have a single entrance point for your web application (e.g. index.php) that handles all of the requests. This code is responsible for loading all of the dependencies, processing the request and sending the response to the browser. The front controller pattern can be beneficial because it encourages modular code and gives you a central place to hook in code that should be run for every request (such as input sanitization).

Model-View-Controller

The model-view-controller (MVC) pattern break up code into logical objects that serve very specific purposes. Models serve as a data access layer where data is fetched and returned in formats usable throughout your application. Controllers handle the request, process the data returned from models and load views to send in the response, and views are display templates (markup, xml, etc) that are sent in the response to the web browser.

MVC is the most common architectural pattern used in the popular frameworks.

Helpful Links:

kfc.i.illuminationes.com Malware attack on WordPress

How this malware affect to your website?
This malware added a infected JS to your website pages and because of that whenever you open page it redirects to somewhere else. If antivirus installed on your machine then it will raise notice that website is harmful to your website.
How to prevent?
First reason for this attack is your password. If your password is weak then it will affect to your website very easily.
First way to avoid this type of malware attack, update your password to strong one so that your password never gets hack.
If your website is already affected with this malware then you need to follow below mentioned steps to remove it from your website.
1. You need to find out the affected files so that you can remove this malware from your website, to find out affected files –
1 Disable all your plugin and themes, and reload your website then checkout that affected JS are are loaded or not. If malware not loaded with your page means that any one of your themes or plugins is affected with this malware.
Now the simple way to remove this affected plugins and themes is manually activate one by one plugin and reload webpage and checkout that malware is loaded or not, so that you can easily find out the affected themes and plugins.
2. Another simple way is – this malware update your file type to some suspected file type. So one way is manually find out suspected file types and remove that suspected files.
3. Third way to remove this malware is simply download all your code on your system and scan your root folder with help of good antivirus, it will easily remove infected files from your code base.
4. Sometime this malware affect your WordPress files so in that case you need update your WordPress to latest version.

Sublime Text on click go to function definition

Sublime text not come up with built in functionality for go to function definition on mouse click event.
Lots of people facing the same issue with Sublime text editor.
We have quick and easy solution for this functionality. We can easily implement this functionality to sublime.
below are the steps – 
1. Create file with name – Default (Windows).sublime-mousemap with file type sublime-mousemap
2. Add this code snippet to created file

 

3. Copy created file to User folder( C:\Users\(Your_User_Name)\AppData\Roaming\Sublime Text 3\Packages\User )
4. Restart Sublime.

Useful Packages For Sublime Text

Sublime text come up with very few packages, we need to manually add packages to make it more useful.
Following are the packages that I found useful for Sublime text.
1. Add Folder To Project.
this packages help you to add multiple code folder to same project.
Go to -> Project -> Add folder to project.
2. All Autocomplete
This packages gives you auto complete popup to each word that you type in Sublime.
3. AutoFileName 
Auto file name gives you list of file name when you type name of any of file which is available in our code folder.
4. Go To Definition
This packages gives you “Go To Definition” option in right click menu. So that you can quickly go o the function defination.
5. Go To Class
This packages add “Go To Class” option to right click menu, So that you can quickly go to the selected class.
6. Go To RowCol
Some code file are so large, and find any specific line in that huge file, its headache for developer.
Go to row col packages help you to jump on the specific line by using line number.
You can to quickly go to particular line number by using this package.
You can use this package by using Ctrl + G key combination.
7. Project manager
This package allow you to create multiple project.

Make your Sublime Text Editor More Sublime

Here are few settings, which makes your sublime more sublime.
Open your setting file. Located at
preferences → Settings – User

1. Caret style

Change your caret style to phase and it will fade in and out rather than blink.

Other options – “smooth”, “phase”, “blink”, “wide” and “solid”.

 

2. Highlight current Line

By default sublime not highlight the current line. Highlighting the current line is a subtle visual cue when jumping back and forth between a browser and code.

This is also super handy when you are dealing with long lines of code/text and you want to quickly know if the current line is wrapped or not.

 

3. Line height

This increases the readability of code so much better.

 

4. Enable Code folding arrows

Code Folding in Sublime Text is great but most of the people don’t use it, Because the arrows aren’t visible until you hover over the gutter.

Make them always visible and get folding!

 

5. Bold Folder Labels

One of the downsides to Sublime Text is that the sidebar isn’t as strong as other editors.
Bold visual helps out here to make Sublime editor more strong. This helps us to quickly identify what is a folder and what is a file.

 

6. Show Tabs and Whitespacs

By default, Sublime shows whitespace on selected text. Tabs show up as dashes, while spaces are dots.

You can control when Sublime shows white space with the draw_white_space setting.
The default value is selection, but you can override that with all to always show whitespace or none to never show it.

If you also want Sublime to automatically replace tab characters with spaces when you open a file, override the translate_tabs_to_spaces setting: