How to Install PDFLib in PHP on WAMP??

PDFLib is use to write in PDF through PHP. PDFlib provides binary file instead of package. I just installed in Windows 7 32 bit system.To Install PDFLib you have to manipulate your php.ini file.

Following are the steps to Install PDFLib:

  • First of all download PDFLib package from where you will get the .dll file.This is the URL Click Here to download PDFLib. PDFLib provides packages for Windows,Linux and Mac also. Look at the image below

 

For Me it is Windows 7 on x86
For Me it is Windows 7 on x86
  • Next step is to extract the zip file
  • After extracting the zip you will get ‘PDFlib-9.1.0-MSWin32-php’ as a folder
  • Open above folder then bind then PHP and open the folder of your PHP version.
  • If you have confusion in your PHP version then I have already shared a guide on how to check your php version, which will help you to get your PHP version.
  • For me my PHP version is 5.6.25.
  • For me it is ‘php-560_VS11’.
  • Open this folder and copy .dll file that is ‘php_pdflib.dll’.
  • Save this file in your extension_dir.
  • To know your extension directory then
    • Open php.ini file
    • Search extension_dir in php.ini file

 

extension_dir in php.ini file my location
extension_dir in php.ini file my location
  • Now open php.ini and add the extension
    extension=php_pdflib.dll
extension=php_pdflib.dll
extension=php_pdflib.dll
  • Restart Apache server
  • To verify that library is added then write below code
    echo class_exists('PDFlib');
    //if true then work is done else post comment below.We will help you
    
  • That is it for installing PDFLib

 

Hope you like it and if you face any problem or have any query then comment below!!!

 

Understanding Design Pattern in PHP

   First of all we have to understand what is Design Pattern?

  • Design patterns is introduced to us by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides.
  • The main concept behind design patterns is to structure code of your project, so that developer can develop building blocks very easily and quickly.
  • Design patterns encapsulate data in friendly terms
  • Design patterns make our code easier to manage and easier to understand and manipulate

There are many design patterns is developed and represented by developers

We will discuss total of 6 commonly use design patterns.

  • Factory Method
  • Singleton Pattern
  • Strategy Pattern
  • MVC (Model View Controller)
  • Chain of command
  • The Front Controller

The Factory Method Design Pattern

Factory method is one of the simplest and most commonly used pattern. Majority of design patterns support loose coupling.
Now, What is loose coupling??
In Object oriented programming, coupling defines that how much a single class is depended to another set of classes. In other words, if we change something in class A then all classes that depends upon class A will have to suffer so this called coupling.

More the dependency tight the coupling,loose coupling means classes are independent.

Coming back to the factory method. To diminish the problem that occurs due to tight coupling we use factory method. You need alternative so that classes can talk to each other but don’t want to tie them.

For Example,

Lets Assume you have a class name User.

Developer used User class around 50 times in his project. He created all the objects of User class with the help of ‘new’ keyword. But all of a sudden developer have to change the name of User class.

Then what will happen??

He has to change the name of his class in 50 different places

$obj = new User();  // Object declaration with old name 
$obj = new Usernewname();  // Object declaration with new name.

This problem is solved with design pattern factory.

Factory.php

interface IUser {
    function getrollno();
}
class User implements IUser {
    public $rollno;
    public function __construct($rollno) {
        $this->rollno = $rollno;
    }
    public function getrollno() {
        return $this->rollno;
    }
}
class Userfactory {
    public static function createobject() {
        return new User(1);
    }
}
$objuser = Userfactory::createobject();
Print_r($objuser->getrollno()); // outputs 1

In above program Interface IUser is defines what user object should do. A factory class is made because in case developer has to rename his class then he just have to change the name in static function of factory class i.e, Userfactory.

public static function createobject() {
        return new Usernewname(1); // Developer just changed name in factory method not in 50 places
    }

Factory Design pattern sometimes overkill the situation but this is the solid way to create an object of a class in any size of project.

The Singleton Design Pattern

Singleton method is used when we have a single instance of a class for entire request lifecycle. For example, During single page application a handle to connect database required only once so it is overhead to use multiple and force application to open and close database connection.

An object in a singleton pattern is single for entire request life cycle.

To  implement singleton pattern using a static variable you have to keep few things in mind

  • __construct() – Declared as protected so that no one can create second instance outside the class with the help of new operator
  • __clone() – This is a magic function and it is used here to prevent cloning of object of a class by declaring it as private
  • __wakeup() – Wake up is also a magic function and it also declare as private so that unserializing of object in class won’t be possible

You  have to include above points to prevent second instance of a class.

Singleton.PHP

class Singleton
{
public static function getObject(){
static $object = null;
         
         if (null === $object) {
            $object = new static();
         }
         return $object;

}
protected function __construct() {
      }
      
      private function __clone() {
      }
      
      private function __wakeup() {
      }

}
$obj = Singleton::getInstance();

Above program implements Singleton based on static method creation getObject().

The Strategy Design Pattern

When we want to manipulate specific families of algorithm  according to our need then  it is basic to use strategy pattern.When  developer has no knowledge how the actual implementation has done then to manipulate algorithm follow strategic pattern.

Strategy.php

When you want to convert array into JSON or serialize it.

interface  Output{
public function load();

}
class SerializeOutput implements Output
{
    public function load()
    {
        return serialize($elements);
    }
}
class JsonOutput implements Output
{
    public function load()
    {
        return json_encode($elements);
    }
}
class ArrayOutput implements Output
{
    public function load()
    {
        return $elements;
    }
}

Now in team other developer can also add there code i.e, new output types with out affecting the main algorithm.

Model View Controller(MVC) Design Pattern

MVC divides three main parts of any application which is interconnected to each other.Many programming languages are following this pattern like Ruby,C#,Java,PHP.

Model

  • Central component of MVC
  • Manage database directly
  • Business logic is being written in model
  • Models store the data and controller demands it.
  • Responds to instructions and manipulate the database

View

  • The main purpose of view is to display data and get data from model
  • Users can see view’s object
  • It may also update model

Controller

  • Controller interact with user and system
  • Controller just interact with user and according to user requirement if needed it goes to model and get the data from model and pass it to appropriate view

Example

Controller File

use yii\web\Controller;
class UserController extends  controller{

public function actionView($user_id){

$model = new User();
$user_id_come_from_model = $model->getUserid($user_id);
return  $this->render(‘userview’,[‘$user_id’=>$user_id_come_from_model]); 

}

}

Model File

use yii\base\Model;
class  User() extends model{
public function getUserid ($user_id){
return $user_id;
}
}

View File

echo $user_id;

First of all above program is written in PHP and Yii2 framework.So, to run you have to install yii2 in your laptop.

Chain of command Design Pattern

Chain Of responsibility is the other name of chain of command pattern. In his design pattern.

  • There is a chain of objects to handle request
  • Concretehandler does not design in such way to handle  a request from client or it does not know how to satisfy the request from client then it passes this request to another handler
  • Client: It makes a request to handler
  • Handler: Tries to satisfy the request.
  • ConcreteHandler: Accepts the request and passes the request if it can’t satisfy the request.

Chain Of responsibility

</pre>
<?php
interface Showroom {

public function get($key);
}

abstract class AbstractShowroom implements Showroom {

protected $_nextHandler;

public function get($key) {
return $this->_nextHandler->get($key);
}

}

class Nearbyshowroom implements Showroom {

protected $_values;

public function __construct(array $values = array()) {
$this->_values = $values;
}

public function get($key) {
return $this->_values[$key];
}

}

class InMemoryNearbyshowrrom implements Showroom {

protected $_nextHandler;
protected $_cached = array();

public function __construct(Showroom $nextHandler) {
$this->_nextHandler = $nextHandler;
}

protected function _load($key) {
if (!isset($this->_cached[$key])) {
$this->_cached[$key] = $this->_nextHandler->get($key);
}
}

public function get($key) {
$this->_load($key);
return $this->_cached[$key];
}

}

class FrontEnd extends AbstractShowroom {

public function __construct(Showroom $nextHandler) {
$this->_nextHandler = $nextHandler;
}

public function getEscaped($key) {
return htmlentities($this->get($key), ENT_NOQUOTES, 'UTF-8');
}

}

// Client code $store = new Nearbyshowroom(array('pd' => 'I10', 'ia' => 'nano', 'ac' => 'tiago', 'hh' => 'Helmut'));

$cache = new InMemoryNearbyshowrrom($store);

$frontEnd = new FrontEnd($cache); echo $frontEnd->get('ia'), "\n";

echo $frontEnd->getEscaped('hh'), "\n";

 

Chain Of Responsibility on Wikipedia

The Front Controller Design Pattern

I am telling you an application without front controller.Lets assume client requests http://www.beingtopper.org/about.php .Now Apache server receives the request and find about.php in directory and execute the file.

Now,what is wrong in this??

If you want to load something that is common to all other files as well but you have to write it in each file as you are not using any front controller.Without front controller code of an application will become messy.

Front controller is the file that recieves all the request first and do some logic that is written and then pass the flow to requested file.

In Codeigniter index.php is the front controller and below is the code of it

define('ENVIRONMENT', 'development');

if (defined('ENVIRONMENT'))
{
	switch (ENVIRONMENT)
	{
		case 'development':
			error_reporting(E_ALL);
		break;
	
		case 'testing':
		case 'production':
			error_reporting(0);
		break;

		default:
			exit('The application environment is not set correctly.');
	}
}

	$system_path = 'system';

	$application_folder = 'application';

	if (defined('STDIN'))
	{
		chdir(dirname(__FILE__));
	}

	if (realpath($system_path) !== FALSE)
	{
		$system_path = realpath($system_path).'/';
	}

	$system_path = rtrim($system_path, '/').'/';

	if ( ! is_dir($system_path))
	{
		exit("Your system folder path does not appear to be set correctly. Please open the following file and correct this: ".pathinfo(__FILE__, PATHINFO_BASENAME));
	}

	define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));

	define('EXT', '.php');

	define('BASEPATH', str_replace("\\", "/", $system_path));

	define('FCPATH', str_replace(SELF, '', __FILE__));

	define('SYSDIR', trim(strrchr(trim(BASEPATH, '/'), '/'), '/'));


	if (is_dir($application_folder))
	{
		define('APPPATH', $application_folder.'/');
	}
	else
	{
		if ( ! is_dir(BASEPATH.$application_folder.'/'))
		{
			exit("Your application folder path does not appear to be set correctly. Please open the following file and correct this: ".SELF);
		}

		define('APPPATH', BASEPATH.$application_folder.'/');
	}

require_once BASEPATH.'core/CodeIgniter.php';


In above front controller below are the important common code is written

  • Different configuration according to different environment
  • System and application path is defined.
  • Last line is used to load the bootstrap file
  • Main constants is being defined of an application

By defining above points you dont need to add this in each file.

So, this the end of the topic design pattern.Hope you liked it.Please comment below if you have any questions!!!