0
0
PHPprogramming~10 mins

Factory pattern in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new instance of the Product class using the factory method.

PHP
<?php
class Product {
    public function getName() {
        return "Product";
    }
}

class Factory {
    public static function create() {
        return new [1]();
    }
}

$product = Factory::create();
echo $product->getName();
?>
Drag options to blanks, or click blank then click option'
AFactory
Bcreate
Cnew
DProduct
Attempts:
3 left
💡 Hint
Common Mistakes
Using the factory class name instead of the product class name.
Forgetting to use the 'new' keyword before the class name.
2fill in blank
medium

Complete the factory method to return a new instance of the correct class based on the type parameter.

PHP
<?php
interface Vehicle {
    public function drive();
}

class Car implements Vehicle {
    public function drive() {
        return "Driving a car";
    }
}

class Bike implements Vehicle {
    public function drive() {
        return "Riding a bike";
    }
}

class VehicleFactory {
    public static function createVehicle($type) {
        if ($type === '[1]') {
            return new Car();
        } elseif ($type === 'bike') {
            return new Bike();
        }
        return null;
    }
}

$vehicle = VehicleFactory::createVehicle('car');
echo $vehicle->drive();
?>
Drag options to blanks, or click blank then click option'
Acar
Bbike
Cvehicle
Ddrive
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'bike' instead of 'car' in the if condition.
Using the class name instead of the string type.
3fill in blank
hard

Fix the error in the factory method to correctly instantiate the requested product.

PHP
<?php
abstract class Button {
    abstract public function render();
}

class WindowsButton extends Button {
    public function render() {
        return "Render Windows button";
    }
}

class MacButton extends Button {
    public function render() {
        return "Render Mac button";
    }
}

class ButtonFactory {
    public static function createButton($os) {
        switch ($os) {
            case 'windows':
                return new [1]();
            case 'mac':
                return new MacButton();
            default:
                throw new Exception("Unsupported OS");
        }
    }
}

$button = ButtonFactory::createButton('windows');
echo $button->render();
?>
Drag options to blanks, or click blank then click option'
AButton
BMacButton
CWindowsButton
DButtonFactory
Attempts:
3 left
💡 Hint
Common Mistakes
Instantiating the abstract Button class.
Using the MacButton class for the windows case.
4fill in blank
hard

Fill both blanks to complete the factory method that creates different notification objects.

PHP
<?php
interface Notification {
    public function send();
}

class EmailNotification implements Notification {
    public function send() {
        return "Sending Email Notification";
    }
}

class SMSNotification implements Notification {
    public function send() {
        return "Sending SMS Notification";
    }
}

class NotificationFactory {
    public static function createNotification($method) {
        if ($method [1] 'email') {
            return new EmailNotification();
        } elseif ($method [2] 'sms') {
            return new SMSNotification();
        }
        return null;
    }
}

$notification = NotificationFactory::createNotification('email');
echo $notification->send();
?>
Drag options to blanks, or click blank then click option'
A===
B==
C!=
D<>
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same equality operator for both conditions.
Using inequality operators instead of equality.
5fill in blank
hard

Fill all three blanks to complete the factory method that creates shape objects and calls their draw method.

PHP
<?php
interface Shape {
    public function draw();
}

class Circle implements Shape {
    public function draw() {
        return "Drawing Circle";
    }
}

class Square implements Shape {
    public function draw() {
        return "Drawing Square";
    }
}

class ShapeFactory {
    public static function createShape($type) {
        return match($type) {
            'circle' => new [1](),
            'square' => new [2](),
            default => throw new Exception("Unknown shape"),
        };
    }
}

$shape = ShapeFactory::createShape('[3]');
echo $shape->draw();
?>
Drag options to blanks, or click blank then click option'
ACircle
BSquare
Ccircle
Dsquare
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up class names and string types.
Using the wrong string for the shape type.