Last active
July 17, 2016 19:51
-
-
Save joffilyfe/f57723f3bcfff3063118befbb5a5d093 to your computer and use it in GitHub Desktop.
Classe e Objetos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Classe e Objetos | |
* Este exemplo mostra de maneira simples como classes são criadas em PHP e | |
* como objetos podem ser instanciados e como métodos são chamados -> | |
*/ | |
class Pessoa { | |
private $nome; | |
private $idade; | |
private $cidade; | |
public function __construct($nome, $idade, $cidade) { | |
$this->nome = $nome; | |
$this->idade = $idade; | |
$this->cidade = $cidade; | |
} | |
public function getNome() { | |
return $this->nome; | |
} | |
public function getIdade() { | |
return $this->idade; | |
} | |
public function getCidade() { | |
return $this->cidade; | |
} | |
public function __toString() { | |
return "Nome: ". $this->getNome() . " Idade: ". $this->idade; | |
} | |
} | |
$nova_pessoa = new Pessoa("Joffily", 26, "Guarabira"); | |
// Chama a função __toString() | |
echo $nova_pessoa, "<br>"; | |
// Chama a função getNome() | |
echo "Meu nome é: ", $nova_pessoa->getNome(), "<br>"; | |
if ($nova_pessoa->getCidade() === "Guarabira") { | |
echo $nova_pessoa->getNome(), ' você mora na melhor cidade do mundo!'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment