Skip to content

Instantly share code, notes, and snippets.

@maks-rafalko
Created November 11, 2019 07:09
Show Gist options
  • Save maks-rafalko/5c263f4d19bbc9a421e4f8025655160c to your computer and use it in GitHub Desktop.
Save maks-rafalko/5c263f4d19bbc9a421e4f8025655160c to your computer and use it in GitHub Desktop.
Salary Strategy
<?php
interface SalaryInterface {
public function getSalary(): float;
}
class DynamicStrategy implements SalaryInterface {
/**
* @var int
*/
private $hours;
/**
* @var float
*/
private $perHour;
public function __construct(int $hours, float $perHour)
{
$this->hours = $hours;
$this->perHour = $perHour;
}
public function getSalary(): float
{
return $this->hours * $this->perHour;
}
}
class FixedStrategy implements SalaryInterface {
/**
* @var float
*/
private $salary;
public function __construct(float $salary)
{
$this->salary = $salary;
}
public function getSalary(): float
{
return $this->salary;
}
}
class Employee {
// ...
public function __construct(/* ... */, SalaryInterface $salaryStrategy)
{
}
public function getSalary(): float
{
return $this->salaryStrategy->getSalary();
}
}
class Developer {}
new Developer('Ivan', new FixedStrategy(1000));
new Developer('Ivan', new DynamicStrategy(123, 4));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment