Skip to content

Instantly share code, notes, and snippets.

@wesllycode
Created August 27, 2023 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesllycode/4cf96a07733497ee1f44afa35f3a7c47 to your computer and use it in GitHub Desktop.
Save wesllycode/4cf96a07733497ee1f44afa35f3a7c47 to your computer and use it in GitHub Desktop.
Explicando o usado da interface com trait. Se observar, eu declarei o método que interface pedi na trait e como trait é chamado dentro da classe Concert.php, por isso não dar erro de falta de implementação do método getPrice().
<?php
require_once 'src/HasMenu.php';
class Concert implements PricingContract
{
use HasMenu;
public function __construct()
{
$this->itens = [
'Beer',
'Ale',
'Nachos'
];
}
}
<?php
trait HasMenu
{
public array $itens = [];
public function getMenu(): array
{
return $this->itens;
}
public function getPrice(){
}
}
<?php
require_once 'src/Concert.php';
$concert = new Concert();
print_r($concert->getMenu());
<?php
interface PricingContract
{
public function getPrice();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment