Interfaces and Traits
Interface Contract
An interface names methods a class promises to provide.
contract
Code can depend on the interface name instead of a specific class name.
Interface Contract
interface_contract.php
Replay: real traced execution (multi-file project)
<?php
interface Formatter
{
public function format(string $name): string;
}
class PrefixFormatter implements Formatter
{
public function format(string $name): string
{
return "hello " . $name;
}
}
$name = "Ada";
$formatter = new PrefixFormatter();
$message = $formatter->format($name);
echo "name=" . $name . "\n";
echo "message=" . $message . "\n";
<?php
interface Formatter
{
public function format(string $name): string;
}
class PrefixFormatter implements Formatter
{
public function format(string $name): string
{
return "hello " . $name;
}
}
$name = "Grace";
$formatter = new PrefixFormatter();
$message = $formatter->format($name);
echo "name=" . $name . "\n";
echo "message=" . $message . "\n";
<?php
interface Formatter
{
public function format(string $name): string;
}
class PrefixFormatter implements Formatter
{
public function format(string $name): string
{
return "hello " . $name;
}
}
$name = "Rasmus";
$formatter = new PrefixFormatter();
$message = $formatter->format($name);
echo "name=" . $name . "\n";
echo "message=" . $message . "\n";
$name ← Ada, $formatter ← PrefixFormatter
15$name→ Ada = "Ada"; //@name="Grace", "Rasmus"16$formatter→ PrefixFormatter = new PrefixFormatter();17$message = $formatterPrefixFormatter->format($nameAda);public function format(string $name): string
8{9 public function format(string $nameAda): string10 {11 return "hello " . $nameAda;12 }$message ← hello Ada
16$formatter = new PrefixFormatter();17$message→ hello Ada = $formatterPrefixFormatter->format($nameAda);1819echo "name=" . $nameAda . "\n";20echo "message=" . $messagehello Ada . "\n";outputname=Ada message=hello Ada
$name ← Grace, $formatter ← PrefixFormatter
15$name→ Grace = "Grace";16$formatter→ PrefixFormatter = new PrefixFormatter();17$message = $formatterPrefixFormatter->format($nameGrace);public function format(string $name): string
8{9 public function format(string $nameGrace): string10 {11 return "hello " . $nameGrace;12 }$message ← hello Grace
16$formatter = new PrefixFormatter();17$message→ hello Grace = $formatterPrefixFormatter->format($nameGrace);1819echo "name=" . $nameGrace . "\n";20echo "message=" . $messagehello Grace . "\n";outputname=Grace message=hello Grace
$name ← Rasmus, $formatter ← PrefixFormatter
15$name→ Rasmus = "Rasmus";16$formatter→ PrefixFormatter = new PrefixFormatter();17$message = $formatterPrefixFormatter->format($nameRasmus);public function format(string $name): string
8{9 public function format(string $nameRasmus): string10 {11 return "hello " . $nameRasmus;12 }$message ← hello Rasmus
16$formatter = new PrefixFormatter();17$message→ hello Rasmus = $formatterPrefixFormatter->format($nameRasmus);1819echo "name=" . $nameRasmus . "\n";20echo "message=" . $messagehello Rasmus . "\n";outputname=Rasmus message=hello Rasmus