Skip to content

Instantly share code, notes, and snippets.

@ArchTaqi
Last active February 12, 2025 11:17
Show Gist options
  • Save ArchTaqi/036b2fd0cba7eab72920c237f592d483 to your computer and use it in GitHub Desktop.
Save ArchTaqi/036b2fd0cba7eab72920c237f592d483 to your computer and use it in GitHub Desktop.
PHP Interview Questions

Implementing a Factory Pattern for Vacation Management in PHP

We are building a vacation management system where different types of vacations (Paid Vacation, Sick Leave, and Work From Home) need to be created dynamically based on user input.

Demo Code (Starter):

Below is an incomplete PHP code snippet. Your task is to complete the VacationFactory class so that it properly creates instances of different vacation types using the Factory Pattern.

Use any online compiler to write code;

<?php

// Step 1: Define an abstract Vacation class
abstract class Vacation {
    protected $type;
    protected $duration;

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

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

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

    abstract public function getDescription();
}

// Step 2: Implement concrete vacation classes
class PaidVacation extends Vacation {
    public function __construct($duration) {
        parent::__construct($duration);
        $this->type = "Paid Vacation";
    }

    public function getDescription() {
        return "You have booked {$this->duration} days of Paid Vacation.";
    }
}

class SickLeaveVacation extends Vacation {
    public function __construct($duration) {
        parent::__construct($duration);
        $this->type = "Sick Leave Vacation";
    }

    public function getDescription() {
        return "You have taken {$this->duration} days of Sick Leave Vacation.";
    }
}

class WorkFromHomeVacation extends Vacation {
    public function __construct($duration) {
        parent::__construct($duration);
        $this->type = "Work from Home";
    }

    public function getDescription() {
        return "You will be working from home for {$this->duration} days.";
    }
}

// Step 3: Implement the VacationFactory class (Complete this!)
class VacationFactory {
    public static function createVacation($type, $duration) {
        // TODO: Implement the Factory Pattern to return the correct Vacation object
    }
}

// Step 4: Client code (This should work once the factory is implemented correctly)
$paidVacation = VacationFactory::createVacation("Paid", 5);
$sickLeaveVacation = VacationFactory::createVacation("SickLeave", 3);
$workFromHomeVacation = VacationFactory::createVacation("WorkFromHome", 7);

echo $paidVacation->getDescription() . PHP_EOL;
echo $sickLeaveVacation->getDescription() . PHP_EOL;
echo $workFromHomeVacation->getDescription() . PHP_EOL;

Implementing Method Chaining in PHP

We have a UserProfile class that allows us to build a user profile with optional posts and comments. The current implementation requires calling methods separately, but we want to improve it using method chaining.

Use any online compiler to write code;

Code Now

use App\Service\UserProfile;

$profile = new UserProfile();
$profile->setUserId(1);
$profile->withPosts();
$profile->withComments();

$result = $profile->getProfile();
print_r($result);

Expected Code

$result = (new UserProfile())
    ->setUserId(1)
    ->withPosts()
    ->withComments()
    ->getProfile();
print_r($result);

Comments are disabled for this gist.