Skip to content

Instantly share code, notes, and snippets.

@pedrochaves
Last active August 29, 2015 14:24
Show Gist options
  • Save pedrochaves/b00b1811104e4ba8aa1f to your computer and use it in GitHub Desktop.
Save pedrochaves/b00b1811104e4ba8aa1f to your computer and use it in GitHub Desktop.
OO: PHP4 vs. PHP5
<?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
<?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