Last active
August 29, 2015 14:24
-
-
Save pedrochaves/b00b1811104e4ba8aa1f to your computer and use it in GitHub Desktop.
OO: PHP4 vs. PHP5
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 | |
class Person | |
{ | |
var $name = ''; | |
public function Person($name) | |
{ | |
$this->name = ucfirst($name); | |
} | |
function getName() | |
{ | |
return $this->name; | |
} | |
} | |
$me = new Person('pedro'); | |
echo $me->getName(); // Imprime "Pedro" | |
$me->name = 'pedro chaves'; | |
echo $me->name; // Imprime "pedro chaves" pois tudo é público por padrão |
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 | |
class Person | |
{ | |
private $name = ''; | |
public function __construct($name) | |
{ | |
$this->name = ucfirst($name); | |
} | |
function getName() | |
{ | |
return $this->name; | |
} | |
} | |
$me = new Person('pedro'); | |
echo $me->getName(); // Imprime "Pedro" | |
$me->name = 'pedro chaves'; // Erro porque $name é privado |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment