Categories
Design Pattern Programming

DI (Dependency Injection) in PHP

There are a few similar terms in the programming world like HTML injection, SQL injection, etc.

What does DI mean?

Dependency Injection is a design pattern that helps avoid hard-coded dependencies for classes.

Let’s try to make it clear using a simple Shopping Cart Example

<?php

class Car
{
    public function buy()
    {
        return true;
    }
}

class MotorBike
{
    public function buy()
    {
        return true;
    }
}

class Cart
{
    protected $vehicle;
    public function __construct(Car $vehicle)
    {
        $this->vehicle = $vehicle;
    }

    public function proceed()
    {
        return $this->vehicle->buy();
    }
}

$cart = new Cart(new Car());
$cart->proceed();

Suppose, You have an online shop and there are 2 products (Car & MotorBike). So you may write code like the above example.

Is there anything wrong with the above example?

Nothing is wrong here. Everything is fine and workable code to purchase Car.

Question: How can you buy a Motorbike?

Answer: It’s very simple. Just Change the Cart Class __construct() function like below.


    public function __construct(MotorBike $vehicle)
    {
        $this->vehicle = $vehicle;
    }

And Initiate the class like below.


$cart = new Cart(new MotorBike());
$cart->proceed();

It works !!!

But the Cart Class depends on the Car/Motorbike class. You resolved this dependency by changing the Cart class constructor.

It violates the Solid Principle (A philosophy of making OOD object-oriented design) by Robert C. Martin.

S - Single-responsiblity Principle
O - Open-closed Principle
L - Liskov Substitution Principle
I - Interface Segregation Principle
D - Dependency Inversion Principle

He is also known as uncle BOB.

Eventually, it violates the Open-closed Principle. That means you can extend your class but you can’t modify it. But you already did that.

What will happen if we can resolve this dependency automatically? This concept is called Dependency Injection.

Let’s try to inject the Class Dependency to the Cart Class Constructor using the power of OOP.

<?php

interface Vehicle
{
    public function buy();
}

class Car implements Vehicle
{
    public function buy()
    {
        return true;
    }
}

class MotorBike implements Vehicle
{
    public function buy()
    {
        return true;
    }
}

class Cart
{
    protected $vehicle;
    public function __construct(Vehicle $vehicle)
    {
        $this->vehicle = $vehicle;
    }

    public function proceed()
    {
        return $this->vehicle->buy();
    }
}

$cart = new Cart(new MotorBike());
$cart->proceed();

$cart = new Cart(new Car());
$cart->proceed();

Check the example above. You can Add Car/Motorbike without modifying your Cart class.

I made an abstraction Layer (Vehicle) of the Lower Module (Car/Motorbike Class) and Changed the Higher Module (Cart Class) Constructor Parameter type to that Abstraction layer.

All (Higher Module & Lower Module ) depends on Vehicle, not each other.

This is known as DI (Dependency Injection)

Categories
Windows

How to disable password prompts during git push/pull

Firstly, Generate an SSH key into your server/localhost

ssh-keygen -t rsa -b 4096 -C "Your Email Address"

You will Get your .ssh/id_rsa.pub. Copy the Key and Add a Aew SSH Key like the above image.

Now register the key with your Github Username

ssh -i ~/.ssh/id_rsa.pub -T your username@github.com

You are Good to go now.

Copy the SSH link and Run it into your server app directory terminal

git clone git@github.com:takielias/tall.git
That’s it. It’s done withour your github username/password.
Categories
Windows

How to resize thousands of images in multiple folders?

I have been working to migrate an Osclass classified website to Custom made Laravel ad listing E-commerce solution for 2 weeks. It has more than 1,20,000 uncompressed preview and thumbline images that cause the slow page load issue. I was desperately seeking a minimal solution to resize thousands of images in multiple folders.

My OS is windows 10. After few hours of google search, got a few applications that serves my partial requirements but xnview (FREEWARE) an awesome application for bulk image resizing that fulfill my needs.

I need to unchanged the Images folder staructure and keep image extension same as source. I could do this by using this application but an afficient way.

Multiple CPU core Suppor

It has multiple CPU core support that makes it blazing fast image resizing tool.

Load image tab

So, let’s start. You may select single image (Add Files) of multiple folders (Add folder) with images and click Next.

Action Tab

Fix your desired setup and click Next

Output Folder Setup

Don’t forget to check the keep folder structure in order to keep the previous folder structure same as source.

Now click Convert and That’s it !!!

Categories
Windows

Change/Modify environment variables without rebooting windows

  1. Search environment in the search bar

Click Edit the System Environment variables & edit as required

2. Open Command Prompt as Administrator

3. Type set PATH=C & Press Enter (This will refresh the environment variables)

4. Close and restart command prompt window

5. Finally, type echo %PATH% & press Enter

This will automatically Refresh your windows environment variables.