Skip to content

Instantly share code, notes, and snippets.

@netojoaobatista
Forked from Bolinha1/Contato.php
Last active December 28, 2015 05:19
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 netojoaobatista/7449164 to your computer and use it in GitHub Desktop.
Save netojoaobatista/7449164 to your computer and use it in GitHub Desktop.
<?php
/**
* Eu já disse que o nome Entidades é muito ruim para um namespace?
*/
namespace Entidades;
class Company
{
private $contacts = array();
private $contactIterator;
public function addContact(Contact $contact)
{
$this->contacts[] = $contact;
}
public function getContactIterator()
{
if ($this->contactIterator == null) {
$this->contactIterator = new ArrayIterator($this->contacts)
}
return $this->contactIterator;
}
}
<?php
/**
* Consigo pensar em poucos nomes tão ruins para um namespace,
* quando Entidades.
*/
namespace Entidades;
class Contact
{
private $id;
private $key;
private $value;
public function __construct($key, $value)
{
$this->key = $key;
$this->value = $value;
}
public function getId()
{
return $this->id;
}
public function getKey()
{
return $this->key;
}
public function getValue()
{
return $this->value;
}
}
use Entidades\Company;
use Entidades\Contact;
$company = new Company();
$company->addContact(new Contact('Telefone Financeiro', '16-3202-xxxx'));
$company->addContact(new Contact('Telefone Comercial', '16-3202-xxxx'));
$company->addContact(new Contact('Site', 'www.empresa.com'));
$company->addContact(new Contact('Facebook', 'facebook.com/empresa'));
$company->addContact(new Contact('Twitter', 'twitter.com/empresa'));
foreach ($company->getContactIterator() as $contact) {
printf("%10s: %s\n", $contact->getKey(), $contact->getValue());
}
@dorianneto
Copy link

Na linha 21 do arquivo Company.php ta faltando um ponto e vírgula e falta uma contra barra na instanciação da classe ArrayIterator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment