Select Page

Factory Method Design Pattern with Example in PHP

Factory Method is one of the creational design pattern. In simple words, we can say: factory method is a creational design pattern that is mainly focused on object creation in an abstract way from sub-classes. It means we will let the sub-class to create object of product concrete class.

When to use Factory Method design pattern

When we don’t know how many types of product or sub classes could be needed.

Make things loosely coupled

When we need to create object from sub-classes instead of product concrete class

Advantage of Factory Method design pattern

The Factory method design pattern makes the code loosely coupled & reuse able. This way we can follow the single responsibility and open, close principle of SOLID. Besides, we don’t need to worry about the enhancement or scalability of services.

Real Life Example in PHP

Let’s take an example of book library, suppose we run a small book library and for now we are selling Programming Books. Within a few months we got popularity and getting many request for storing Mathematic Books. In future we may have much more requests for more genres. That is really good for the business.

So here we don’t know beforehand what we may need in future. So if we write code only taking care of programming related books selling logics then we may encounter to refactor code again. Even we may need to write entire logics again for other genres. That is really bad 🙁

So let’s jump into the code and see how can solve above discussed problem:

First we will create an abstract class that will contain a factory method like below:

abstract class BookFactory
{
    abstract public function createBook($type): GenericBook;
}

This class is basically called Factory Class. Since it’s containing an abstract method so the class that will be derived from it will must implement this method & follow the signature. It means the return type of createBook method will be an interface of GenericBook.

Now let’s create a sub-class that is basically Factory Concrete Class. The class which is responsible for creating object of Product Concrete class.

class Book extends BookFactory
{
    protected $name, $author;

    public function __construct($name, $author)
    {
        $this->name = $name;
        $this->author = $author;
    }

    public function createBook($type): GenericBook
    {
        try {
            return new $type($this->name, $this->author);
        } catch (\Throwable $th) {
            throw $th;
        }
    }
}

So all is going good, we have implemented createBook method with proper signature & this is a constructor method for setting up basic things. We are creating object here on fly on the run time, beforehand we don’t know that object we really want to create but as long as it’s returning GenericBook interface all is good. Through the interface we can enforce the Product Concrete Class to implement required methods.

Let’s create the interface:

interface GenericBook
{
    public function bookName(): string;

    public function bookAuthor(): string;

    public function price();
}

Now let’s create Product Concrete Class that contains logics about particular genre books.

class ProgrammingBook implements GenericBook
{
    private $bookName, $bookAuthor;
    private $price = '$120';

    public function __construct($name, $author)
    {
        $this->bookName = $name;
        $this->bookAuthor = $author;
    }

    public function __get($name)
    {
        echo $name . ' prop is not available, try method';
    }

    public function bookName(): string
    {
        return $this->bookName;
    }

    public function bookAuthor(): string
    {
        return $this->bookAuthor;
    }

    public function price()
    {
        return $this->price;
    }
}

Now our code is entirely following Factory Method Design Pattern and is loosely coupled. Below way client will use the class:

$book = new Book('PHP Book', 'Rasmus Lerdorf');
$programmingBook = $book->createBook('ProgrammingBook');
echo $programmingBook->bookName() . '<br>';
echo $programmingBook->bookAuthor() . '<br>';
echo $programmingBook->price() . '<br>';

This way client may create as many programming book as they need.

Now If we need more genre books then simple we can create another class and implement all logics to that particular class and don’t need to touch anything else. Even we can take advantage of common things that already have on the ProgrammingBook class.

Here is how to achieve it:

class MathBook extends ProgrammingBook
{
    private $price = '$90';

    public function price()
    {
        return $this->price;
    }
}

Supposed we need to sell Math books & we have all things same as programming book but price is different, so we can simple create a class for MathBook and extends ProgrammingBook. Since price is different we can override from particular class, that is really cool 😀

That’s all for the Factory Method, don’t forget to comment your recommendation/suggestion.

MySQL group_concat explanation with real example

MySQL group_concat is an aggregate function that join multiple strings into one single string. Some time we may need to show retrieve data as a unit. For example we can take WordPress post and categories. A post can have multiple categories. So if we want to retrieve a single post along with it’s associative categories then we have to make a JOIN Query here like this:

SELECT
  p.ID,
  p.post_title,
  p.post_author,
  term.name
FROM
  lms_posts as p
  INNER JOIN lms_term_relationships as term_rel ON term_rel.object_id = p.ID
  INNER JOIN lms_terms as term ON term.term_id = term_rel.term_taxonomy_id
WHERE
  p.ID = 4588

Above SQL query is fine and should get data like below:

MySQL result may vary, by the way main point is here we can see four rows. But it would be great if we can get all categories in one column instead of multiple rows.

So update the above query with group_concat:

SELECT
  p.ID,
  p.post_title,
  p.post_author,
  group_concat(term.name) as category
FROM
  lms_posts as p
  INNER JOIN lms_term_relationships as term_rel ON term_rel.object_id = p.ID
  INNER JOIN lms_terms as term ON term.term_id = term_rel.term_taxonomy_id
WHERE
  p.ID = 4588

So now we can see the result is absolutely fine

All the categories in one column. We don’t need to loop through multiple rows anymore. Hope it is really helpful 😀

How to share session between domain & sub-domain in Laravel

Sometime you have need to share session between domain & sub domain the way I was need it. I had searched a little and finally fixed my problem. That’s why I am going to share the solution with you all. Few steps that you have to do:

1) make sure to keep same app domain name that is located IN config/session file
2) change session driver (file to database) in config/session file
3) change session driver from .env file
4) keep the both site same app id in .env file

Then Command:
PHP ARTISAN SESSION::TABLE
PHP ARTISAN MIGRATE
above command will create a table in your database

that is all, hope this will solve your problem. If you have any issue comment be below section.

Receive Incoming Webhook Data

Today I gonna share how can we receive data from webhook in PHP. It’s just a cinch but sometime feels really tricky.

Webhook: 

Webhook is very useful way of getting update from external system. It notify when something happened on webhook server side. Webhook client is for receiving  this update.

Receive Webhook Data:

Data from external source thrown on JSON format to receive in PHP follow below code snippets and use this file url as delivery url in webhook settings.

$json = file_get_contents(‘php://input’); //receiving json data
$data = json_decode($json, true); //decoding json

$file = fopen(‘text.txt’, “w+”);//creating file
fwrite($file, “Response: “.json_encode($data));//writing data on it full body

fclose($file);

Open text.txt file and see magic 🙂