I suggest to study object orientation more deeply, the example below is very superficial and teaches little.
First we need to create a class with the name pais
.
namespace lib
/**
* Classe Pais
* @package lib
*/
class Pais
{
/**
* @var string $iso;
*/
private $iso;
/**
* @var string $nome;
*/
private $nome;
/**
* Atribui um valor a propriendade $iso
* @param string $iso
* @return lib\Pais
*/
public function setIso($iso)
{
$this->iso = $iso
$return $this;
}
/**
* Retorna o valor da propriedade $iso
* @return string
*/
public funcion getIso()
{
return $this->iso;
}
/**
* Atribui um valor a propriendade $nome
* @param string $nome
* @return lib\Pais
*/
public function setNome($nome)
{
$this->nome = $nome
$return $this;
}
/**
* Retorna o valor da propriedade $nome
* @return string
*/
public funcion getNome()
{
return $this->nome;
}
}
We first assign a namespace
to the class. Namespace allows the grouping of classes to avoid conflict between their names, acting as an encapsulator, that is, its operation is equivalent to that of directories in operating systems, where two files with the same name can not exist in a single directory, but there may be two files with the same name located in different directories.
Notice that the class has comments and some started with @
. These comments serve to standardize and generate automatic documentation for your code, using tools like phpdoc . In addition to the documentation, the comments serve to help programmers who may in the future change their code, making it easier to understand.
Notice that in the class we define the get
and set
methods for each property. Hence a natural question: Why create methods for accessing variables, if we can access them directly? - For security reasons.
The private
variables can only be accessed from within the class, that is, they can not be accessed outside the scope of the class. For example, imagine that within the setIso
method we would have a check if the code actually exists; if the property were public
, we could simply set any value to the iso
property without any validation.
class Pais
{
// Propriedade pode ser acessada fora do escopo da classe Pais
public $iso;
}
$pais = new Pais();
$pais->iso = 'QualquerValorParaIso';
When attempting to access a private property outside the scope of the class, the following error occurs:
class Pais
{
// Propriedade não pode ser acessada fora do escopo da classe Pais
private $iso;
}
$pais = new Pais();
$pais->iso = 'QualquerValorParaIso';
Error: Can not access private property lib \ Country :: $ iso (500 Internal Server Error)
Clarified (hopefully) some concepts, we will now create a new file where we will use the class Pais
.
test.php
use lib\Pais
$paisA = new Pais();
$paisA
->setName('Brasil')
->setIso('BRA')
;
$paisB = new Pais();
$paisB
->setName('Portugal')
->setIso('PRT')
;
$paisC = new Pais();
$paisC
->setName('Portugal')
->setIso('PRT')
;
The above code generates 3 instances of class Pais
, that is, 3 distinct objects independent of each other, each with its scope.
In a very simple way, we can verify that the defined values are equal between objects using the getIso
method:
$isoA = $paisA->getIso(); //BRA
$isoB = $paisB->getIso(); //PRT
$isoC = $paisC->getIso(); //PRT
//output: não
echo 'Iso país A==B', $isoA==$isoB ? 'sim':'não';
//output: não
echo 'Iso país A==C', $isoA==$isoC ? 'sim':'não';
//output: sim
echo 'Iso país B==C', $isoB==$isoC ? 'sim':'não';
The above method is ineffective if there is a collection of countries, test all combinations by generating a row for every if
or even we may not know how many items there are in the collection, etc ... I used the above method just to demonstrate how to create an instance of the class (which results in an object) and use its methods.
Each case is a case and should be studied in order to be implemented in the best possible way, but for the already good example.