Object-Oriented Programming
Object
"Object is anything existing in the real world, in
concrete or abstract format, that is, that exists physically
or just conceptually."
Object incorporates
- data structure : characteristics (data) that are attributes
- behavior : functions that are the methods
Manipulation
"The Object must manipulate methods and attributes belonging only to it"
Example
class Pessoa{
private $nome;
private $idade;
public function __construct($nome, $idade){
$this->nome = $nome;
$this->idade = $idade;
}
public function setNome($novoNome){
$this->nome = $novoNome;
}
public function getNome();
public function getNomeDoMeio();
}
What is not object orientation
Objects that have relation, that must be put as attributes, but that are
positions as dependents.
Example
class Endereco{
protected $rua;
protected $bairro;
public function setRua($rua){
$this->rua = $rua;
}
}
class Pessoa extends Endereso{
private $nome;
private $idade;
public function __construct($nome, $idade, $rua, $bairro){
$this->nome = $nome;
$this->idade = $idade;
$this->rua = $rua;
$this->bairro = $bairro;
}
}
In your system you can define that "every person has an address",
but that does not mean that it should extend/contain an address,
the person is a person whether or not he or she has an address.
How to relate objects
Some objects
- Reptiles
- Mamifero
- Animal
- Whale
- Lizard
Relationship
- Animal -> Mamifero -> Whale
- Animal -> Reptiles -> Lizard
That is, the animal class must contain generic methods and attributes
to accommodate both classes, but nothing specific for a particular class.
Manipulating attributes
The attributes belong to the class or within the Voce class
can and should manipulate freely, but when an Extendo object wishes
capture or change its value should use the appropriate methods,
that are not necessarily the get or set.
Example
class Conta{
private $saldo;
public function getSaldo();
public function depositar($valor){
$this->saldo += $valor;
}
public function retirar($valor){
if ($this->saldo >= $valor){
$this->saldo = $this->saldo - $valor;
}else{
echo "Saldo insuficiente!!!";
}
}
}
Completion
Analyze the attributes of each class well and always keep them as private
or protected
,
creating appropriate methods for their manipulation.
OBS
"Leaving the Object orientation and doing something similar is easy." - Professor
Related: What good is a builder? and When to use Setters and Getters?
– rray
Welcome to the world of Object Orientation! It’s cool this interest in learning a paradigm that is so used nowadays! Do not be shy in asking questions: a person who asks is silly for 5 minutes. The one who does not ask can be foolish for the rest of life.
– Pedro Laini
True, today I may be breaking my head to learn the concepts, but you can be sure that tomorrow I will be understanding and helping those next to you who are in doubt. Thank you @Pedrolaini
– Leandro Macedo
Thanks @rray. Already helping a lot what you recommended me!
– Leandro Macedo
Poor guy, another one who fell for the OOP story :)
– Maniero