Proxy is a class that is merely an interface to something else. It may be an interface to anything; from being a network connection, a file, a large object in memory, or any other resource that is too difficult to duplicate.
In our example here, we will simply be creating a simple proxy to forward to one of two objects depending on how the proxy is instantiated.
Accessing a simple Proxy class allows the client to access both feeders for cats and dogs from one object, depending on whether it's been instantiated.
Let's start off by defining an interface for our AnimalFeeder
:
<?php namespace IcyApril\PetShop; interface AnimalFeeder { public function __construct(string $petName); public function dropFood(int $hungerLevel, bool $water = false): string; public function displayFood(int $hungerLevel): string; }
We can then define two animal feeders for a cat and a dog:
<?php namespace IcyApril...